Type constraint for diamond operator in generics

I really appreciate all redesign in Scala 3, but its generic syntax remains weird with []. I am aware that is some syntax limitations due type constraint syntax in Scala generics. Then, I would like suggest these 2 syntax for type constraint in Scala 3 for consideration.

// 1 - positional deference
trait Sample<T:Upper> ...
trait Sample<Lower:T> ...

// 2 - variance + constraint
trait Sample<+T:Upper> ... 
trait Sample<-T:Lower> ... 

Personally I’m glad that the syntax for type parameters, which I use all the time, is easier to type than the syntax for indexing into a collection, which I almost never do.

If it matters, I believe Go also uses square brackets for type parameters.

What does that mean?

How does the compiler know that T is a type variable and Upper is an existing name I’m using as a bound? What if I have a type in scope called T and I want Sample to have a type parameter named Lower or Upper? (Which might also happen to be existing names.)

And what if I want to have an upper and lower bound?

What about context bounds, will they still require square brackets?

Should the two syntaxes coexist in Scala 3? Or are you asking to make breaking changes for Scala 4?

What if I want to have a lower bound on a covariant type parameter or an upper bound on a contravariant type parameter?

Are these suggestions mutually exclusive or meant in combination?


imo [] is a much more pleasing syntax, I would instead replace Tuple with *.
(Int, Int, Int) to Int * Int * Int

Hi Nafg, yes they are breaking changes, they are two alternative suggestions (only one to be pick). In my view, considering all breaking changes in Scala 3, and considering some changes this would be tiny change I think now it would be a good opportunity to implement this.

They would be replacers to:

trait Sample[T :> Upper]
trait Sample[T <: Lower]

Of course, I am aware, syntax is a very subjective topic, I am just sharing some subjective preferences… anyway, thanks for your reply.