I'd like to be able to do something like this (won't actually work):
class A[T <: B | C]
... and expect this to be valid:
new A[B]
new A[C]
... and this to yield compiller error:
new A[D]
Is something like this possible?
We use <T> to create a generic class, interface, and method. The T is replaced with the actual type when we use it.
A Generic class simply means that the items or functions in that class can be generalized with the parameter(example T) to specify that we can add any type as a parameter in place of T like Integer, Character, String, Double or any other user-defined type.
Generic became part of C# with version 2.0 of the language and the CLR, or Common Language Runtime. It has introduced the concept of type parameters, which allow you to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code.
You could use implicits:
trait AArg[T]
class A[T](implicit e: AArg[T])
implicit val argB = new AArg[B] { }
implicit val argC = new AArg[C] { }
although this doesn't prevent someone from creating an implicit val of type AArg[D]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With