Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala type-level identity function in a type parameter

Tags:

types

scala

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?

like image 858
n8han Avatar asked Feb 04 '11 05:02

n8han


People also ask

What is the meaning of => in Scala?

=> 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 .

What is identity function in Scala?

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.

What is a type parameter in Scala?

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.

How does Scala determine types when they are not specified?

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.


1 Answers

Try a higher-kinded parameter:

class Foo[A[_]] { ... }

type Id[A] = A

type Foo1 = Foo[Option]
type Foo2 = Foo[Id]
like image 117
Daniel Spiewak Avatar answered Oct 01 '22 19:10

Daniel Spiewak