Let constructors to be considered as functions

Currently constructors and functions are not equal.

There is no easy way to pass constructors as function arguments.

So, we can’t do this:

class Pair(a: Int, b: Int) {
  // ...
}

def a(f: (Int, Int) => Pair) = {
  // ...
}

a(new Pair) // boom

It would be very convenient to be able to pass a constructor as a function argument easily.

For example, Swift can do it. A constructor can always be turned into a function with Typename.self shortcut.

A syntax like new Typename _ may be used for disambiguation when it’s required but just new Typename should work fine in most of the cases because it’s possible to infer semantic (instantiation or addressing constructor as a function) from the context.

11 Likes

You can do

 a(new Pair(_, _)) 
2 Likes

I can but it’s not convenient.

3 Likes

Maybe Pair should be a case class?

case class Pair(a: Int, b: Int) {
  // ...
}

def a(f: (Int, Int) => Pair) = {
  println(f(3, 4))
}

a(Pair) // even 'new' is not needed here

prints:

Pair(3,4)

However, since Dotty tries to make instantiation of case classes and other types of classes the same ( see: http://dotty.epfl.ch/docs/reference/other-new-features/creator-applications.html ) then maybe it should be extended also to lifting constructor invocations to functions.

2 Likes

See https://github.com/lampepfl/dotty/pull/7207

2 Likes

Maybe I misunderstand something but seems like this P/R does not allow to use non-case class constructors where functions are expected - there is a negative testcase even.

This unfortunately doesn’t work for higher arities – imagine writing a new MyServiceImpl(_, _, _, _, _, _, _, _, _, _) for a hypothetical MyServiceImpl with 10 arguments. Also whenever the arity of the constructor changes this invocation will have to be updated.

Pair could be a case class, but making a hypothetical MyServiceImpl that’s a service class not a data class, a case class just to get an .apply would be an abuse of case classes.

That’s correct. I just gave the link for context.

1 Like

Eta expansion for creator applications feature request

4 Likes