I can declare an abstract type such as
type A[B]
and in a subclass define that as
type A[B] = Option[B]
if I want A to be an Option. And if I want A to be B itself, I can do this:
type A[B] = B
Can I achieve the same thing with type parameters instead of type members?
=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .
The identity function's type parameter is A , which like the value parameter a is simply a unique identifier. It is used to define the type of the value parameter a and the return type of the function.
Language. Methods in Scala can be parameterized by type as well as by value. The syntax is similar to that of generic classes. Type parameters are enclosed in square brackets, while value parameters are enclosed in parentheses.
For example, a type constructor does not directly specify a type of values. However, when a type constructor is applied to the correct type arguments, it yields a first-order type, which may be a value type. Non-value types are expressed indirectly in Scala.
Try a higher-kinded parameter:
class Foo[A[_]] { ... }
type Id[A] = A
type Foo1 = Foo[Option]
type Foo2 = Foo[Id]
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