- Professional Scala
- Mads Hartmann Ruslan Shevchenko
- 240字
- 2021-07-23 17:24:23
Base Syntax
Now that we can use REPL, let's understand the base Scala syntax. For now, it's not necessary to learn it in detail, but let's get familiar with it by using an example.
Note
For a formal, detailed description, refer to the SLS: Scala Language Specification here: http://scala-lang.org/files/archive/spec/2.12/.
Base Syntax for Definitions
Scala compilation unit – This is a set of definitions inside an entity (template-entity), which can be an object, a class, or a trait. We will speak about the Object-Oriented part of the Scala language in detail later. Now, let's look at the basic syntax. Let's define some classes in REPL:
> class X { def f():Int = 1 } > Class X defined // answer in REPL
Definitions inside the entity can be nested entities, functions, or values:
> def f():Int = 1
Here, the function f
is defined, returning 1
. We will talk about this function in detail in Chapter 3, Functions. Now, let's stay on the top-level view:
> val x = 1
Here, the value x
is defined with value 1
:
> var y = 2
Here, the mutable variable y
is defined with value 2
.
Other high-level entities include objects and traits. We can create objects by writing object or trait definitions:
> object O { def f():Int =1 } > trait O { def f():Int =1 }
We will talk about classes, objects, and traits in the next chapter.
Now, let's look at defining an object in REPL with the name ZeroPoint.
Steps for Completion:
- 廣電5G從入門(mén)到精通
- 物聯(lián)網(wǎng)網(wǎng)絡(luò)安全及應(yīng)用
- 從區(qū)塊鏈到Web3:構(gòu)建未來(lái)互聯(lián)網(wǎng)生態(tài)
- INSTANT PhpStorm Starter
- 物聯(lián)網(wǎng)檢驗(yàn)檢測(cè)技術(shù)
- Web Application Development with R Using Shiny
- SD-WAN架構(gòu)與技術(shù)(第2版)
- 互聯(lián)網(wǎng)基礎(chǔ)資源技術(shù)與應(yīng)用發(fā)展態(tài)勢(shì)(2021—2023)
- 物聯(lián)網(wǎng)時(shí)代
- WordPress Web Application Development
- Microsoft Dynamics CRM 2011 Applications(MB2-868) Certification Guide
- 網(wǎng)絡(luò)工程實(shí)施技術(shù)與方案大全
- Hands-On Microservices with Node.js
- 異構(gòu)蜂窩網(wǎng)絡(luò)關(guān)鍵理論與技術(shù)
- 5G智慧交通