DarkStar DarkStar - 3 months ago 14
Scala Question

Scala "type" alias confusion

I am working on Coursera Assignment and in Week 2. This week assignment is not tough but very confusing.

I wrote below code and it works fine

def union(s:Set[Int], t:Set[Int]):Set[Int] = s union t


But if I use type to create alias of Set and rewrite above as

type Set = Int => Boolean
def union(s:Set, t:Set):Set = s union t


Now I get error for union is not a member of Set

Answer
def union(s:Set[Int], t:Set[Int]):Set[Int] = s union t

This works because Set[T] defines a function named union, which is called in the above code.

type Set = Int => Boolean
def union(s:Set, t:Set):Set = s union t

This doesn't work because functions don't have a method named union.

Comments