Is the whole function or expression or object in scala?

I'm confused.

I thought all this expression because the operator returns a value. But I also heard that all this is an object in scala.

What is it really? Why did scala decide to do this anyway? What does this mean for the scala developer?

+6
source share
3 answers

I thought all this expression because the operator returns a value.

There are things that do not matter, but for the most part this is correct. This means that we can basically discard the distinction between “expression” and “expression” in Scala.

However, the term "return value" does not quite fit. We say that everything "evaluates" meaning.

But I also heard that all this is an object in Scala.

This does not contradict the previous statement at all :) It simply means that every possible value is an object (therefore, each expression evaluates the object). By the way, features like first-class citizens in Scala are also objects.

Why did scala decide to do this anyway?

It should be noted that this is actually a generalization of the Java path, where expressions and expressions are two different things, and not all are an object. You can translate every part of Java code to scala without a lot of adaptations, but not vice versa. Thus, this constructive solution makes scala actually more powerful in terms of conciseness and expressiveness.

What does this mean for the scala developer?

This means, for example, that:

  • You often don't need to return , because you can just put the return value as the last expression in the method
  • You can use the fact that if and case are expressions that shorten your code.

Example:

 def mymethod(x: Int) = if (x > 2) "yay!" else "too low!" // ... println(mymethod(10)) // => prints "yay!" println(mymethod(0)) // => prints "too low!" 

We can also assign the value of such a compound expression to a variable:

 val str = value match { case Some(x) => "Result: " + x case None => "Error!" } 
+13
source

The difference here is that the statement “everything is an expression” is made about blocks of code, while “everything is an object” is made about the values ​​in your program.


Code blocks

In Java, there are expressions and expressions. That is, any “block” of code is an expression or expression;

 //the if-else-block is a statement whilst (x == 4) is an expression if (x == 4) { foo(); } else { bar(); } 

Expressions are of type; no applications; Operators call solely for their side effects .

In scala, every block of code is an expression; that is, it has type:

 if (x == 4) foo else bar //has the type lub of foo and bar 

It is very important! I cannot stress this enough; this is one of the things scala is good at working with because I can assign a value to an expression.

 val x = if (x == 4) foo else bar 

Value

By value, I mean that we can refer in the program:

 int i = foo(); //i is a reference to a value java.util.TimeUnit.SECONDS; 

In Java above, i not an object - it is a primitive. In addition, I can access the SECONDS TimeUnit field, but TimeUnit is not an object either; it is static (due to the lack of a better phrase). In scala:

 val j = 4 Map.empty 

As for the language, j is an object (and I can send methods to it), just like a Map is a module (or an accompanying object) for type scala.collection.immutable.Map .

+5
source

Is the entire function or expression or object in scala?

Nothing.

There are things that are not objects. for example classes, methods.

There are things that are not expressions. for example, class Foo { } is an operator and does not evaluate any value. (This is basically the same point as above).

There are things that are not functions. I do not need to mention any examples for this, since there would be a lot of space in any Scala code.

In other words, “Everything is a X” is nothing more than a sales pitch (in the case of Scala).

+5
source

Source: https://habr.com/ru/post/912455/


All Articles