[JAVA] I touched Scala

introduction

image.png

I started learning about Scala, so I will write it as a memorandum. The development environment is Mac OS Mojave ver 10.14.6.

Building an environment for Scala for Mac

Java installation

You may not need to write this anymore. Please refer to the official and necessary Qiita articles as appropriate and install the latest version.

sbt installation

Easy for MacOS

brew install sbt

You can install it just by typing.

Then the console will appear with the following command. It's a good idea to lower the directory before launching the console. When you start the console, a directory called target will appear, so if you do not want to pollute the current, it is recommended to lower the directory.

sbt console

When you want to quit

scala> :quit

It is OK if you enter.

Scala basics

Hello,World!

Now let's start console and see if scala works.

println("Hello, World!")
>>>Hello, World!

Writing println is similar to Java. Scala is a language that has static types like Java, and the type is checked when the code is executed.

Be careful about handling variables

Of course, Scala also has the concept of variables, but it's important to note that there are two types of Scala variables: val and var.

val: Once a value is stored in a variable, it cannot be changed var: modifiable (like regular variables in C or Java)

In Scala, programming is basically done only with val. (This also has a functional feel ...)

val x = 3 * 2
>>>x: Int = 6

It will be. The difference from Java etc. is that the variable type is not declared. This is because the compiler automatically infers the type of x from the type of the calculation result of 3 * 2.

This is called (local) type inference. Also, by using val, let's confirm that an error occurs when handling it as another type and that variables cannot be changed (reassigned).

//Declaration of new variables
var x = 3 * 3
>>>x: Int = 9

x = "Hello, World!"
>>>
<console>:8: error: type mismatch;
 found   : String("Hello, World!")
 required: Int
       x = "Hello, World!"
           ^

x = 3 * 4
>>>x: Int = 12

//Experiment with variables that cannot be changed
val y = 3*2
>>>y: Int = 6


y = 3*3
>>>
<console>:12: error: reassignment to val
       y = 3*3
         ^

Of course, you can also explicitly declare the type in Scala.

'val'Or'var' <Variable name> : <Model name> = <formula>
//Example
val x: Int = 3 * 3

The result is like this. You may be tempted to write "= " here, because Scala is a functional type. Basically, functional languages have only expressions.

About the features of Scala

What is Scala

"Scala is a programming language developed by Professor Martin Odersky of the Swiss Federal Institute of Technology Lausanne (EPFL) in 2003. Scala is unique in that it can perform both object-oriented and functional programming. Also, the processing system is Since it runs on the JVM, 1. Most of the Java language libraries can be used seamlessly. "

... apparently ...

As we saw earlier, Scala is similar to Java but functional. In other words, it seems to be a language like Thoroughbred, which is object-oriented and is a functional programming language.

Being functional

The language Scala is an object-oriented concept that interprets and incorporates functional functions. As a result, functional functions are realized without unnecessarily bloating the language specifications.

Also, as an important feature of functional languages, we must understand topics related to expression evaluation and concepts such as invariance, referential transparency, and purity. (I will omit it, keeping in mind that it is important this time.)

Java compatibility

"Scala is a language that is primarily compatible with Java. Scala types and method calls are compatible with Java, and Java libraries can be used normally from Scala."

In other words, it seems that you can fully use the existing Java library! Thank you.

Asynchronous programming, parallel / distributed programming

"In Scala, the Future that expresses asynchronous computation is included in the standard library and is used in various libraries. Asynchronous programming can handle a large number of simultaneous access of clients that exceeds the number of threads. I will. "

It seems that it is a language that also supports the popular parallel and distributed programming.

end

That's all for installing Scala on your Mac and running simple commands. There are many aspects that are different from ordinary programming languages, and I'm still new to it.

Next time, I will study compilation, installation of Intel iJ, and basic usage.

reference

This document is CC BY-NC-SA 3.0

image.png It is distributed under.

https://dwango.github.io/scala_text/

Recommended Posts

I touched Scala
I touched Scala ~ [Class] ~
I touched Scala ~ [Object] ~
I touched Scala ~ [Trait] ~
I touched Scala ~ [Control syntax] ~
I first touched Java ③
I first touched Java ④
I first touched Java
[Scala] Constructor
I made an eco server with scala
Note that I touched Android's SQLiteOpenHelper lightly
I touched ODM for Developer ② Rule development
I touched on the new features of Java 15