Hawk66 Hawk66 - 1 year ago 69
Scala Question

Equivalent of C# type constraint (where) in Scala

In C# you can define a method

public T GetComponent<T>() where T : IComponent

to express that T has to be of IComponent.

How can I express the same in Scala?

Answer Source

In Scala, this is called an Upper Type Bound (as you can also have a Lower Type Bound, a constraint that doesn't exist in C#). You define them like this:

def getComponent[T <: IComponent]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download