Unable to make type aliasing work with Function1

This is my code

  type MyFunc1[A] = Function1[_,A]
  val toIntFunc : MyFunc1[Int] = (a : Double) => a.toInt

This compiles fine. But when I try out the code


The compilation fails with the error

type mismatch, expected _$1, actual Double

Isn’t Function1[_,A] telling that it is a function of anything (NOT Any) to A?

That _ is an existential type, which wasn’t your intent.

To partially apply a Scala 2 type like this, you need to use a type lambda. The syntax for that is awkward, so in practice, everyone uses https://github.com/typelevel/kind-projector.

In Scala 3, this will be part of the language.

1 Like

Thank you. Can I know what the awkward syntax is?

There are two ways to adjust types with aliases, one as you tried, one that’s more inline.

type FunkyFunc1[A] = Function1[String,A]

the inline way makes use of reflection and structural types

({ L[A] = Function1[String,A]})#L

Both of which are on the front-page of the kind-projector github page. :slight_smile:

Only on style usage (side comment on _):

I was thinking more of eta postfix notate to raise a method to a function.

def foo = ???
val fooFn = foo _

To my eyes, it is too close to foo_. The wildcard is an issue too, but generally is easier My me to identify. I’m not very experienced, it might not be a big deal to other people.

The style guide:

A brief introduction to _:

IIRC, that the underscore for eta expansion is mostly (entirely?) going away in Scala 3.

1 Like