Accepted answer

The keyword type in Scala creates an alias for a given type. For example:

scala> type Str = String
defined type alias Str

scala> val greeting: Str = "Hello World!"
greeting: Str = Hello World!

This is very similar to what you did:

scala> type Set = Int => Boolean
defined type alias Set

scala> val isEven: Set = _ % 2 == 0
isEven: Int => Boolean = <function1>

scala> println(isEven(4))

scala> println(isEven(5))

Although type aliases may sometimes be useful for clarification purposes, documentation is not their primary use case. Scala's type system is very sophisticated. For instance there is an alternative to generics, namely abstract types. Consider this:

// Generics
abstract class GenericAbstraction[TypeArgument]
class GenericConcrete extends GenericAbstraction[String]

// Abstract types
abstract class TypeAbstraction {
   type TypeArgument
class TypeConcrete extends TypeAbstraction {
   type TypeArgument = String

These code samples basically accomplish the same thing, but there are situations where you need abstract types, but can't (or shouldn't) use generics. You can find more information here.


I am considering like this. The function definition in the type is abstract, which makes it possible to define different concrete methods to implement them. I think it is called Polymorphism or later binding. which makes it possible to bind the function type and concrete method later in the runtime.

I think in JAVA, you cannot override an abstract method by static method. But in functional language, it seems very naturally to define concrete method to implement the abstract function. Please correct me if I am wrong.

Look at this about "polymorphic-functions".

But the conclusion is sad to me. We can only simulate it in scala. Check this from the link.

"Wow, what a journey. We saw that it’s not possible to directly define polymorphic functions in Scala. Instead there are some work arounds to kind of simulating them. It all comes down to the fact, that a function is a value of a certain Function type, which needs to be type parameterized at runtime, that is all type parameters need to be type parameterized for getting a real value (or instance of that type)."

"So as a last conclusion, defining polymorphic functions is possible, but the consequences might outweight the benefits. As always, you need to be aware of the given risks and decide for yourself if it’s worth the trade offs (which i hope to have shown to you) for your concrete problem area …"


You can define a function literal as follows:

val incrementor = (x: Int) => x + 1

or if you have some context that can be used by Scala's type inference, you can use reduced forms like:

val listOfInt = List(1, 2, 3, 4, 5)

listOfInt map {x => x + 1}
listOfInt map {_ + 1}

or even

listOfInt map {1 +}

These literals all imply the type themselves or have their type constrained by the expected type of a higher order function they are being passed to.

There have been several questions on SO about the difference between functions and methods which would be good background reading but perhaps taking a look at the free version of Martin Odersky's book Programming in Scala (Version 1) would be a much better starting point to read up about functions and methods.

Related Query

More Query from same tag