Quantcast
Channel: generic type
Viewing all articles
Browse latest Browse all 4

Scala: How to define a generic method parameter that must extend a base type

$
0
0
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.”


Viewing all articles
Browse latest Browse all 4

Trending Articles