functional programming - Scala expression evaluation -
I'm amazed with the following Scala compiler behavior when I declare the type unit function, but still the body has a function Provides an evaluation for an int, Scala compiler is fine with it. Def add (x: int, y: int) = x + y def main (arguments: array [string]): unit = {add (args (0) .toInt, args ( 0) .toInt)}
The same is not true with other types, such as
def afunc: string = {1} // type mismatch ; Found: int (1) requirement: string with this if I
def afunc: unit = {1} or
DIF main (args: array [string]): unit = {2 + 2} // which is the first addition above In both cases I get the following warning:
A pure expression does nothing in the state of the statement; You may have to leave the required brackets There are 2 questions in one sense. What is the difference between the difference of a function that evaluates int and expression 2 + 2? Then why does the compiler not complain on the Earth that there is a function for evaluation of the unit which evaluates another type, because it happens between other types
matar
P> What is the difference between a function that evaluates the value of int and expression 2 + 2 Does.
A function that evaluates an int can be a side effect
var a = 0 def fn: int = {a = a + 1 1} A call for change in each call changes.
So why on earth, the compiler does not complain that a function that estimates the evaluation of the unit receives a body that evaluates other types, Is between.
When you specify the unit as a return type, the compiler gives the function to the unit (only one unit value is because it is an object, then you can call it Scala Can think of as zero type).
Comments
Post a Comment