--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I don’t know who Brian is or what’s not interesting about it. But you can lift the constructor to a function.
scala> trait TT { type T; val ctor: Int => T }
defined trait TT
scala> class TTT(val x: Int)
defined class TTT
scala> new TT { type T = TTT; val ctor = new TTT(_) }
res1: TT{type T = TTT} = $anon$1@59e5ddf
Another way of looking at it is that a constructor of T that takes an Int is a special case of a function Int => T.
To make that more concrete, what if my class is defined like this:
class C(val x: String)
object C {
def apply(i: Int) = new C(i.toString)
}
People use the Int factory often. It’s a common pattern for users of the above fictional library to write C(10)
etc. instead of new C("10")
.
Now, would you want to disallow using C
as your T
? Why is the direct constructor more special than the convenience factory?
My solution allows both. Why isn’t that better?
You could also just write the longer form, (i: Int) => new C(i)