scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.83k stars 1.05k forks source link

pre-SIP: Underscore Syntax for Type Lambdas #5379

Open odersky opened 5 years ago

odersky commented 5 years ago

We'd like to eventually use F[_] as a short-hand syntax for the type lambda [X] => F[X]. It would mean we have to switch regular wildcard syntax to F[?]. The problem is that this is the syntax currently used by KindProjector, because _ was unavailable. The tricky bit is how to shift things around?

We need a multi-version strategy for this. I am starting the version count at 3.0, but it could also be something else, 2.14, or even 2.13.

If type lambdas get introduced in 2.13, and 2.14, we could also start this process sooner.

kubukoz commented 5 years ago

So, just to be clear, in Scala 3.3:

Functor[Either[String, _]] will be the equivalent of

Functor[[X] => Either[String, X]], or what's currently known (with kind-projector) as

Functor[Either[String, ?]]? Sounds good to me. Especially with the suggested roadmap of incremental changes.

SethTisue commented 5 years ago

it's too late for native type lambdas to make 2.13, but 2.14 seems like a strong possibility

SethTisue commented 5 years ago

(for those who haven't seen or thought about this before, https://github.com/lampepfl/dotty/issues/2041 is an earlier iteration of this that explains the motivation more and has some discussion)

Jasper-M commented 5 years ago

Why not do "_is deprecated for wildcards" in 3.0 and then do the last 2 changes a version earlier?

smarter commented 5 years ago

Why not do "_is deprecated for wildcards" in 3.0 and then do the last 2 changes a version earlier?

We'd like to be able to cross-compile code between 2.14 and 3.0 without warnings (but their might be better ways to achieve this, such as getting 2.14 to understand ?)

Krever commented 5 years ago

Functor[Either[String, _]] will be the equivalent of Functor[[X] => Either[String, X]]

I would rather expect [X] => Functor[Either[String, X]] to be precise

smarter commented 5 years ago

@Krever but that wouldn't match how the underscore syntax works for term lambdas (foo(bar(a, _)) is equivalent to foo(x => bar(a, x)), not x => foo(bar(a, x))).

Krever commented 5 years ago

My bad, nevermind ;)

nightscape commented 5 years ago

Wouldn't it be possible that kind-projector uses another symbol first and deprecates and removes the ? before Scala 3.0 is out?

smarter commented 5 years ago

@nightscape Yes.

nightscape commented 5 years ago

Created an issue in kind-projector: https://github.com/non/kind-projector/issues/84

LPTK commented 5 years ago

For the record, a cool use case for type-level underscore syntax I mentioned on the forum.

Being able to write:

def foo[A: _ <:< Int] = // ...

instead of:

def foo[A](implicit ev: A <:< Int] = // ...

or instead of the following, which already works but is clumsy:

def foo[A: [T] => T <:< Int] = // ...
esarbe commented 5 years ago

I would prefer * for wildcard types. Would that be possible?

odersky commented 2 years ago

We should try to make the next step (deprecation of wildcards now).

smarter commented 2 years ago

Before that I'd like to see Scala 2 support the new syntax by default (right now it's still under -Xsource:3, but using a type named ? without backticks has been deprecated for a while so I think it's safe to change the default). Also 3.2 is already slated to deprecate non-exhaustive pattern bindings without case (https://github.com/lampepfl/dotty/pull/14294) and I think we should try not to pile up too many disruptive changes at once, so I'd vote for deprecating only in 3.3

smarter commented 2 years ago

I'd like to see Scala 2 support the new syntax by default

PR: https://github.com/scala/scala/pull/9990