Value Type Set = Int => Boolean in Scala

I do not understand why Set , defined in this way, produces these results.

I understand that Set - it's just a function that takes an int and returns a Boolean value.

Can someone explain to me why I get this result with set?

I think this is a short way to express a function in Scala, but I am new to this language and I do not understand how it works.

 object sets { type Set = Int => Boolean var a=Set(3) //> a : scala.collection.immutable.Set[Int] = Set(3) a(2) //> res0: Boolean = false a(3) //> res1: Boolean = true a(1) //> res2: Boolean = false } ] = Set ( object sets { type Set = Int => Boolean var a=Set(3) //> a : scala.collection.immutable.Set[Int] = Set(3) a(2) //> res0: Boolean = false a(3) //> res1: Boolean = true a(1) //> res2: Boolean = false } 
+6
source share
2 answers

The type that you specified in type Set = Int => Boolean , and the object that you created in var a=Set(3) , is not actually related to each other. Even it works:

 scala> type Set = String defined type alias Set scala> val a = Set(3) a: scala.collection.immutable.Set[Int] = Set(3) 

When you call Set(3) , you call the method apply to the object of Set . If you add the keyword new , it will take into account your nickname, type:

 scala> val b = new Set() b: String = "" ) scala> val b = new Set() b: String = "" 

a(2) , a(3) , a(1) work because Set[A] , actually implements the feature function A => Boolean , and the method apply equivalent contains : http://www.scala-lang.org /api/2.10.3/index.html#scala.collection.immutable.Set

+10
source

I agree that izstva say that they are different. But has a value type Set = Int => Boolean. In the next steps in the console after scala "type Set = Int => Boolean" error disappears.

 scala> def func(s:Set) = 1 <console>:10: error: type Set takes type parameters def func(s:Set) = 1 ^ scala> def func(s:Set[Int]) = 1 func: (s: Set[Int])Int <---- specify the type in the set scala> type Set = Int => Boolean defined type alias Set scala> def func(s:Set) = 1 <---- no error func: (s: Set)Int scala> def getSet(s:Set) = s getSet: (s: Set)Set scala> getSet(Set(1,2)) res0: Set = Set(1, 2) scala> Set(1,2) res1: scala.collection.immutable.Set[Int] = Set(1, 2) scala> val t=Set(1,2) t: scala.collection.immutable.Set[Int] = Set(1, 2) ) = scala> def func(s:Set) = 1 <console>:10: error: type Set takes type parameters def func(s:Set) = 1 ^ scala> def func(s:Set[Int]) = 1 func: (s: Set[Int])Int <---- specify the type in the set scala> type Set = Int => Boolean defined type alias Set scala> def func(s:Set) = 1 <---- no error func: (s: Set)Int scala> def getSet(s:Set) = s getSet: (s: Set)Set scala> getSet(Set(1,2)) res0: Set = Set(1, 2) scala> Set(1,2) res1: scala.collection.immutable.Set[Int] = Set(1, 2) scala> val t=Set(1,2) t: scala.collection.immutable.Set[Int] = Set(1, 2) [Int]) = scala> def func(s:Set) = 1 <console>:10: error: type Set takes type parameters def func(s:Set) = 1 ^ scala> def func(s:Set[Int]) = 1 func: (s: Set[Int])Int <---- specify the type in the set scala> type Set = Int => Boolean defined type alias Set scala> def func(s:Set) = 1 <---- no error func: (s: Set)Int scala> def getSet(s:Set) = s getSet: (s: Set)Set scala> getSet(Set(1,2)) res0: Set = Set(1, 2) scala> Set(1,2) res1: scala.collection.immutable.Set[Int] = Set(1, 2) scala> val t=Set(1,2) t: scala.collection.immutable.Set[Int] = Set(1, 2) Boolean scala> def func(s:Set) = 1 <console>:10: error: type Set takes type parameters def func(s:Set) = 1 ^ scala> def func(s:Set[Int]) = 1 func: (s: Set[Int])Int <---- specify the type in the set scala> type Set = Int => Boolean defined type alias Set scala> def func(s:Set) = 1 <---- no error func: (s: Set)Int scala> def getSet(s:Set) = s getSet: (s: Set)Set scala> getSet(Set(1,2)) res0: Set = Set(1, 2) scala> Set(1,2) res1: scala.collection.immutable.Set[Int] = Set(1, 2) scala> val t=Set(1,2) t: scala.collection.immutable.Set[Int] = Set(1, 2) ) = scala> def func(s:Set) = 1 <console>:10: error: type Set takes type parameters def func(s:Set) = 1 ^ scala> def func(s:Set[Int]) = 1 func: (s: Set[Int])Int <---- specify the type in the set scala> type Set = Int => Boolean defined type alias Set scala> def func(s:Set) = 1 <---- no error func: (s: Set)Int scala> def getSet(s:Set) = s getSet: (s: Set)Set scala> getSet(Set(1,2)) res0: Set = Set(1, 2) scala> Set(1,2) res1: scala.collection.immutable.Set[Int] = Set(1, 2) scala> val t=Set(1,2) t: scala.collection.immutable.Set[Int] = Set(1, 2) ) = s scala> def func(s:Set) = 1 <console>:10: error: type Set takes type parameters def func(s:Set) = 1 ^ scala> def func(s:Set[Int]) = 1 func: (s: Set[Int])Int <---- specify the type in the set scala> type Set = Int => Boolean defined type alias Set scala> def func(s:Set) = 1 <---- no error func: (s: Set)Int scala> def getSet(s:Set) = s getSet: (s: Set)Set scala> getSet(Set(1,2)) res0: Set = Set(1, 2) scala> Set(1,2) res1: scala.collection.immutable.Set[Int] = Set(1, 2) scala> val t=Set(1,2) t: scala.collection.immutable.Set[Int] = Set(1, 2) ) scala> def func(s:Set) = 1 <console>:10: error: type Set takes type parameters def func(s:Set) = 1 ^ scala> def func(s:Set[Int]) = 1 func: (s: Set[Int])Int <---- specify the type in the set scala> type Set = Int => Boolean defined type alias Set scala> def func(s:Set) = 1 <---- no error func: (s: Set)Int scala> def getSet(s:Set) = s getSet: (s: Set)Set scala> getSet(Set(1,2)) res0: Set = Set(1, 2) scala> Set(1,2) res1: scala.collection.immutable.Set[Int] = Set(1, 2) scala> val t=Set(1,2) t: scala.collection.immutable.Set[Int] = Set(1, 2) ( scala> def func(s:Set) = 1 <console>:10: error: type Set takes type parameters def func(s:Set) = 1 ^ scala> def func(s:Set[Int]) = 1 func: (s: Set[Int])Int <---- specify the type in the set scala> type Set = Int => Boolean defined type alias Set scala> def func(s:Set) = 1 <---- no error func: (s: Set)Int scala> def getSet(s:Set) = s getSet: (s: Set)Set scala> getSet(Set(1,2)) res0: Set = Set(1, 2) scala> Set(1,2) res1: scala.collection.immutable.Set[Int] = Set(1, 2) scala> val t=Set(1,2) t: scala.collection.immutable.Set[Int] = Set(1, 2) ) scala> def func(s:Set) = 1 <console>:10: error: type Set takes type parameters def func(s:Set) = 1 ^ scala> def func(s:Set[Int]) = 1 func: (s: Set[Int])Int <---- specify the type in the set scala> type Set = Int => Boolean defined type alias Set scala> def func(s:Set) = 1 <---- no error func: (s: Set)Int scala> def getSet(s:Set) = s getSet: (s: Set)Set scala> getSet(Set(1,2)) res0: Set = Set(1, 2) scala> Set(1,2) res1: scala.collection.immutable.Set[Int] = Set(1, 2) scala> val t=Set(1,2) t: scala.collection.immutable.Set[Int] = Set(1, 2) 
+1
source

All Articles