I have a type like this
sealed class Foo[A](val value: A)
object Foo {
def apply[A](v: A)(implicit num: Numeric[A]): Foo[A] =
/* highly complex stuff to make a Foo[A] */
implicit def toA[A](x: Foo[A]) = x.value
}
Foo
as a class is only supposed to hold the value, so an implicit Numeric would not make much sense. But I need the type of A
to be always a numeric.
So my idea is to just make it impossible to use Foo
s normal constructor outsid开发者_开发知识库e of its companion. Is that possible in Scala?
Yeah, since the companion object can access private members of its companion class you can just make the primary (and auxiliary if any) constructor private. Pseudo code here:
class ConcreteFoo private (n: Int) extends Foo(n)
精彩评论