Allow specification of a subset of type parameters

For option 3 vs option 4: I don’t think having the author have to update the definition site to allow setting the order is a terrible thing, for two reasons:

  1. It’s what we have to do now
    Though partial type application is annoying to implement, it is very possible to get the behavior today, and it’s currently dependent on the method definition
  2. The author is going to have to be aware of the utility of this anyway so the order allows for prefixes

It does also have the advantage of limiting this new behavior to locations where it is explicitly desired, which may be helpful if doing this generally is expensive in terms of resulting compilation times.

Theoretically option 5 is already implemented but available as an experimental feature Named Type Arguments.

So to be able to use it you need to be using a -SNAPSHOT or -NIGHTLY version of the compiler and explicitly enable the feature by either adding the -language:experimental.namedTypeArguments compiler flag or adding the import import language.experimental.namedTypeArgument.

And as it seems hardly anyone uses this feature it might still be not very well tested.


I wasn’t aware that it was an experimental feature. I thought it was dropped completely.

As an aside, if you go to the source through the scaladoc you see the 2.13.7 source code instead of the patched scala 3 version.

Does that mean that option 4 is effectively available through the same path?

Not really. If you want to provide only some of type parameters, the parameters have to be named. If you use positional type parameters you can only either specify all of them or none.

But one nice way of achieving curried type parameters that wasn’t mentioned in this thread but could be useful in some cases is usage of extension methods where you can add type parameters both to the extension and to the method. E.g.

extension [I](a: I)
  def myPlus[O](b: I)(using Into[I, O]): O = ???

val x = MyInt(1).myPlus[MyLong](MyInt(2))
val y = myPlus(MyInt(1))[MyLong](MyInt(2))

Thanks. That’s a good point about using extension methods. That doesn’t quite fit my original (unwritten) motivation, which is to write nice DSLs. Having to change invocation syntax in DSLs may or may not defeat the point of the DSL.

To double-check: I can theoretically already invoke def foo[T, U](...) with foo[T = Int] where U will be inferred by the compiler, but not simply foo[Int]? That seems like a potentially easy change. Thanks for the pointer.

1 Like

Yes, you can do that but you have to be aware of the limitations that usage of experimental features brings (especially if you’re designing a non-internal library nobody else will be able to use it in production).
And in your particular case I think the most elegant solution (and not requiring any usage of experimental language features) would be to slightly redesign your API so that one would rather write something like

myPlus(MyInt(1), MyInt(2)).as[Long]

instead of

myPlus[Long](MyInt(1), MyInt(2))

You could use partial application to effectively split your function into two function calls, one with inferred parameters and one without. See here (and others in that area)

I was hopeful that the clause intervweaving SIP was going to be an answer for this problem, but it has been amended so that it is not possible to specify multiple consecutive type parameter lists. IMHO, the all-or-none nature of type parameters is a major headache for Scala, and one where Rust is strictly better. Is there any strong opposition to Option 2 in my proposal here (using _ or ? to in type argument lists to indicate “infer me” type parameters)?

1 Like

I recently discovered that extension methods can do what you want.

trait F[A,B]

given F[Int,Float]

extension [A,B,C](ab: Either[A,B])
  def get[D](using F[D,C]): C = ???

val ab: Either[String, Char] =???
ab.get[Int] //return type Float

Thanks, someone else had already pointed that out earlier and I responded. Extension methods require dot-call syntax and also require the library writer to know which arguments the user might specify instead of inferring up front. A type-level equivalent of summon/implicitly (as Rust has) has neither of those limitations.

I think named type arguments will be the idiomatic solution for that. Named type arguments increase readability whereas the alternatives (i.e. some wildcard marker, or curried type arguments) decrease it. So we should go all in with this, and do it soon. I am in train of improving the proposal in the link to allow named type arguments everywhere, not just in method calls.


I see. I both disagree that named typed arguments are more readable and also don’t understand why named type arguments, curried type arguments, and wildcard markers need to compete. IMHO, options 2-5 are all available for regular parameters, so why not make them available for type parameters?

However, I see that the ship is likely about to set sail on this one so I’ll just wait patiently for named type arguments!

I expect named type arguments are going to be extremely annoying to use in practice. One of the primary uses I’ve seen in the wild for type currying is specifying an effect type, and type currying works extremely well for situations like this.

Assuming a method with a fairly simple signature for something like this: def foo[F[_], A](fa: F[A]): F[A]

Type currying or multiple adjacent type parameters work nicely because the kind of the types that can be inferred and those that cannot are different, so it’s very hard to screw up something like foo[EitherNel[String,*]](fa).

Named type arguments just adds cognitive load on the user, because now you have to remember what the library maintainer chose as the argument type name. For example, foo[F = EitherNel[String,*]](fa) vs foo[FE = EitherNel[String,*]](fa).

It also converts changes to type argument names from source-compatible to source-incompatible.

Proponents of named type arguments have argued that this just means library authors will need to choose more descriptive type names, which is strictly worse. In this scenario, not only does the user have to deal with remembering what the author chose, they have to deal with typing the thing out: foo[EffectError = EitherNel[String,*]](fa).

Some of this is going to be alleviated by tooling improvements, but why go with something that’s more annoying (but can be made less so) than enabling something which already works, people are already familiar with, and is already supported by the tooling?

I also broadly agree that named type parameters are orthogonal to type parameter partial application. Term parameters come in both named or positional variants, either of which can be fully applied, partially applied (via _), or fully inferred (via implicit) or partially inferred (via implicitly). I use all permutations of these on a regular basis

I don’t see any reason why the same use cases, ergonomics, and affordances wouldn’t apply to type parameters as well. We already have fully applied type parameters, partially applied type parameters (via _), fully inferred type parameters. That proposal adds named type parameters, and it would be a nice synmetry to have both named and positional partially inferred type parameters, symmetric to how implicitly gives us partially inferred term parameters


It’s a cultural problem, not a technical one. So far, there was no way to allow partial type inference (except for the obscure Helper-with-apply method pattern). Now we have two candidates. Both require some degree of buy in.

Curried type parameters require changing the API to support them and worrying about the order in which type parameters should be given. I note that this will not always be the most readable order. As an example, let me just point to the Either debacle. Many languages have a usable result type where of course the result comes first (eg. std::Result in Rust). Scala has Either where the order of parameters is swapped. This is a major roadblock to understanding! Everybody who says different is suffering from expert- (not to say Stockholm-) syndrome. Where did it come from? It came from Haskell’s Either where by convention the result is also written second. Why was that? Because it was deemed that it would be better for specifying the error type first while still allowing the result type to vary (in inference or constructors). So curried type parameters have already contributed to one of the major library design fails in Scala even though they don’t exist yet! And if we make them official no doubt more fails will follow, since the cultural influence from Haskell is very strong.

Named type parameters don’t require the same tradeoffs; they work out of the box with existing architectures. Over time, they will influence API design in the direction that more descriptive names of type parameters will be preferred. I believe this is unequivocally a good thing.

Unlike curried type parameters, named type parameters will also work for constructors, so they are more orthogonal.

I am not against opening up curried type parameters at some point (since at the moment, once we admit type parameter interleaving, it actually takes work to prevent them), but only after named type parameters are firmly established as the default choice for partial inference. Before it’s just not safe. It would split the library ecosystem with a large portion of libraries going in a direction that would make Scala more obscure and difficult to read.


Can the project to unify type parameters and type members not be revived? This was originally the lead feature for Scala 3.0. Even if we can’t move to immediate unification I think any changes should be made in that context. And that unification justifies breaking changes.

1 Like

Sorry if I’m not following, but would there be any problem in Scala if the result were on the left? If you could specify a prefix of type arguments, I think you would do

sealed abstract class Either[+T, +E] 
final case class Left[+T, +E](value: E) extends Either[T, E]
final case class Right[+E, +T](value: T) extends Either[T, E]

where the only confusing thing would be the swapped arguments on Right, which people would rarely notice. Similarly, with currying you would do

sealed abstract class Either[+T, +E] 
final case class Left[+T][+E](value: E) extends Either[T, E]
final case class Right[+E][+T](value: T) extends Either[T, E]

Can you say more about that (sorry if it’s been said elsewhere). Term parameters can be curried in constructors, so why not type parameters?

1 Like

This is actually something I didn’t consider! When I imagine partial type parameter inference, i was thinking something like this:

val foo = Right[String, Implicitly](123) // Right[String, Int]

where Implicitly is a magic type analoguous to the implicitly method we use for term parameters. The name can be tweaked, but jt would provide a way to partially infer type parameters without currying or named params

1 Like

Ah I see. But summon or implicitly is actually a simple library function, whereas the typelevel analogue would have to be built in. And it’s an “implicit” only in a rather wide sense of the word.

But in any case, once we have named type arguments, it’s an open question whether we want to do this as well.