Last active
August 8, 2023 13:55
-
-
Save cvogt/8672a3fd97bc97714822 to your computer and use it in GitHub Desktop.
Default values for type parameters in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> :paste | |
class :=[T,Q] | |
trait Default_:={ | |
/** Ignore default */ | |
implicit def useProvided[Provided,Default] = new :=[Provided,Default] | |
} | |
object := extends Default_:={ | |
/** Infer type argument to default */ | |
implicit def useDefault[Default] = new :=[Default,Default] | |
} | |
scala> def foo[T](implicit default: T := Int, ct: reflect.ClassTag[T]) = ct.toString | |
scala> foo | |
res0: String = Int | |
scala> foo[String] | |
res1: String = java.lang.String |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment