10

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!

Community
  • 1
  • 1
Clark Bao
  • 1,743
  • 3
  • 21
  • 39
  • Your grammar is convoluted and the word choice is strange or plain wrong. For instance `is there a special need that you prefer to define` doesn't make much sense, and `need` there probably should have been `requirement`. Other examples are `naive` at the top and `target` (goal) on the only question you make. That's the other problem here: a question should be a question. It ends in `?`, and it is stated as a question. Here, the question is in the middle of the text, the middle even of a paragraph. I did not downvote you, but you are not being clear. – Daniel C. Sobral Sep 14 '11 at 19:36
  • @Daniel,I have updated the question.You are free to fix the wrong grammer and word and anything you want to correct. – Clark Bao Sep 15 '11 at 04:01
  • @Daniel,Actually I am just not clear how to use these kind of function type in the correct case. Such as type Set2 = (Int,Int,String) => (Boolean => Int). It can be even more complicated ones. – Clark Bao Sep 15 '11 at 04:07
  • @Danial I am looking for real life use case for them and also the correct grammer of impplementation of them.For eg. type Set3 = (Int,Int,String) => (Boolean => Int) => (Boolean => Int) – Clark Bao Sep 15 '11 at 04:08

3 Answers3

29

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.

agilesteel
  • 16,775
  • 6
  • 44
  • 55
1

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.

Don Mackenzie
  • 7,953
  • 7
  • 31
  • 32
  • Thanks, Don.Your answer is cool by showing me some function literals. But I am more interested when you will use "type Set = Int => Boolean". This grammer looks cool and special in functional language. I mainly interested in how to use it in correct case not just use it for learning it. – Clark Bao Sep 11 '11 at 09:31
  • As @agilesteel explained, the type keyword is used to provide an alias to a type. Where it becomes valuable is when the aliased type is complex and therefore cumbersome to use in declarations and the alias leads to more compact and / or intuative expressions, for example type PF = PartialFunction[Int,Int] or type Position = Tuple2[Int, Int] – Don Mackenzie Sep 11 '11 at 13:53
  • Yes.I am clear with it. And what I have tried println(classOf[Set]) it prints out "interface scala.Function1". And I opened Function1, it is defined as a trait. So it is only a type of trait? which is really interesting. – Clark Bao Sep 11 '11 at 14:00
0

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

Clark Bao
  • 1,743
  • 3
  • 21
  • 39