This works
def func(f: => Int) = f
This dosn't (inside class for example)
type EmptyFunct = => Int
or
type EmptyFunct = (=> Int)
Scala version 2.9 Two questions:
For example, a type constructor does not directly specify a type of values. However, when a type constructor is applied to the correct type arguments, it yields a first-order type, which may be a value type. Non-value types are expressed indirectly in Scala.
A nullary or niladic function.
=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .
=> Int is not exactly a function without parameter, it is an Int argument with call by name passing convention. (Of course, that is rather fine a point, as it is implemented by passing a function without parameter ).
The function without argument is written () => Int
. You can do type EmptyFunct = () => Int
.
It is not a type. Inside func, f will be typed as an Int. An argument of type () => Int will not.
def func(f: => Int) = f *2
func (: => Int) Int
But
def func(f: () => Int) : Int = f*2
error: value * is not a member of () => Int
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With