Skip to content

Instantly share code, notes, and snippets.

@cvogt
Last active August 8, 2023 13:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cvogt/8672a3fd97bc97714822 to your computer and use it in GitHub Desktop.
Save cvogt/8672a3fd97bc97714822 to your computer and use it in GitHub Desktop.
Default values for type parameters in Scala
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