In Haskell, I can check a function's type in ghci
by using :t
:
Prelude> :t odd
odd :: Integral a => a -> Bool
This says that the function odd
takes an Integral
type and returns a Bool
. Is there something similar for Scala?
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.
=> 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 .
In the main() function, we created four variables var1, var2, var3, var4 initialized with some values. After that, we get the type of variables using the getClass method and print the result on the console screen.
Scala functions are first-class values. Scala functions are first-class values. You must mention the return type of parameters while defining the function and the return type of a function is optional. If you don't specify the return type of a function, the default return type is Unit.
It's actually the exact same in the Scala REPL:
scala> def odd(x : Int) = x % 2 != 0
odd: (x: Int)Boolean
scala> :t odd
(x: Int)Boolean
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