Clark Bao
Clark Bao

Reputation: 1693

How to use the function type in scala within defined in type meaningfully?

I am new and naive to scala. Just know how to define a function type such as Set here(only as an example).

type Set = Int => Boolean 

def set(i: Int): Set = n => n == i 
def contains(s: Set, i: Int) = s(i)

I also read the wiki of language-agnostic function type. It seems C#,C,Haskel also have the similiar grammer. http://en.wikipedia.org/wiki/Function_type.

My question is in which case you prefer to define one of this kind of abstract type function and use it, And no other choice to reach the same target? Comparing to directly define a concrete method using def

Or I can loose the requirement, to say using this function type, I can make the code looks much better. So that I can know more about function type.

Here my main interested part is type Set = Int => Boolean ,when you want to abstract it out? I am looking for real life use case, and how to implement it in concrete method in scala grammer. For example, this one is a bit complex.

type Set2 = (Int,Int,String) => (Boolean  => Int) => (Boolean  => Int).

I know it's called higher-kinded types. The grammer itself is indeed meaningful. But I just need more plain real life examples to scala beginners.

I found this answer describing for it. What is a higher kinded type in Scala?

But it still looks a bit obscure for me. I prefer plain answer for beginner. It seems like the function itself didn't require anything except the parameter and result type to the implementation mentod. For example, if the result (Boolean) doesn't come from parameter (Int) ,it still compiles.

def set(i: Int): Set1 = aa => new Date().getDate() == i 

Am I unstanding it right?

Let me know why this question is not clear or bad, so I can improve it,Sir!

Upvotes: 10

Views: 12336

Answers (3)

Clark Bao
Clark Bao

Reputation: 1693

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

http://gleichmann.wordpress.com/2011/01/23/functional-java-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 …"

Upvotes: 0

agilesteel
agilesteel

Reputation: 16859

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))
true

scala> println(isEven(5))
false

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.

Upvotes: 29

Don Mackenzie
Don Mackenzie

Reputation: 7963

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.

Upvotes: 1

Related Questions