Instead of reflection or structural types, consider using your types companion object as a function value,
scala> case class UserId(val id: Long) extends AnyVal
defined class UserId
scala> def anotherMethod[T, U](ctor: T => U, t: T) = ctor(t)
anotherMethod: [T, U](ctor: T => U, t: T)U
scala> anotherMethod(UserId, 3L)
res0: UserId = UserId(3)
This works as shown for case classes because the Scala compiler will automatically provide the companion object with an apply
method which invokes the classes primary constructor and it will also arrange for the companion to extend the appropriate FunctionN
trait.
If, for whatever reason, your type can't be a case class you can provide the apply method yourself,
object UserId extends (Long => UserId) {
def apply(l: Long) = new UserId(l)
}
or you can use a function literal at call sites instead,
anotherMethod(new UserId(_: Long), 3L)