Scala: How to define a generic method parameter that must extend a base type
alvin
August 9, 2017 - 7:09pm
In today’s installation of “how to have fun with Scala,” if you want to define a method that takes a parameter that has a generic type, and want to further declare that the parameter must extend some base type, use this syntax:
def getName[A <: RequiredBaseType](a: A) = ???
That example says, “The parameter a
has the generic type A
, and A
must be a subtype of RequiredBaseType
.”