Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking Function Type in Scala

Tags:

scala

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?

like image 231
Vlad the Impala Avatar asked Feb 20 '12 00:02

Vlad the Impala


People also ask

How does Scala determine types when they are not specified?

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.

What is the meaning of => 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 .

How do you find the type of a variable in Scala?

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.

What is the type of a function in Scala?

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.


1 Answers

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
like image 109
Sean Nilan Avatar answered Sep 24 '22 06:09

Sean Nilan