- Learning Scala Programming
- Vikash Sharma
- 492字
- 2021-06-30 19:07:48
What is underneath a Scala program?
A Scala program is a tree of nested definitions. A definition may start with a keyword, definition's name, a classifier, and if it's a concrete definition, then also an entity to which that definition is bound. So the syntax is regular, just like any other programming language has keyword/name/classifier/bound-entity. Let's take an example. We'll use Scala REPL to see how a simple Scala program is built. For that, let's import a Scala package named universe:
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
This import clause brings all the definitions within the universe package in scope. It means that the required functions we are going to use are in scope, and available for us to use. Then we'll use a reify method, which returns an Expr to construct tree out of our simple Scala program expression. We passed a Scala class to our reify method. Let's pretend that a Scala class encapsulates some members like a value with the name segment and a name definitions. We'll talk about all these members as we go along in subsequent sections. For now, let's execute this line and see what we get in response:
scala> val expr = reify {class Car {val segment="SUV"; def name="Q7"}}
expr: reflect.runtime.universe.Expr[Unit] =
Expr[Unit]({
class Car extends AnyRef {
def <init>() = {
super.<init>();
()
};
val segment = "SUV";
def name = "Q7"
};
()
})
The preceding code shows Scala's reify method's response. It looks like alien code (only for now) that we have no idea about, so let's find out what's meaningful to us. We know which is somewhat using the class Car that we passed to generate some code. We recognize this Car class and that it's extending some construct named AnyRef. Every class we define in Scala is a subclass of AnyRef, hence we can see the interpreter has shown the explicit view of our class definition with modifiers, constructors, and members that we defined. We'll use the showRaw(expr.tree) method to print tree:
scala> showRaw(expr.tree)
res0: String = Block(List(ClassDef(Modifiers(), TypeName("Car"), List(), Template(List(Ident(TypeName("AnyRef"))), noSelfType, List(DefDef(Modifiers(), termNames.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(typeNames.EMPTY), typeNames.EMPTY), termNames.CONSTRUCTOR), List())), Literal(Constant(())))), ValDef(Modifiers(), TermName("segment"), TypeTree(), Literal(Constant("SUV"))), DefDef(Modifiers(), TermName("name"), List(), List(), TypeTree(), Literal(Constant("Q7"))))))), Literal(Constant(())))
Now, we'll take a closer look at the response res0. The expression started with Block, that's a tree representing the class we defined. Our class Car contained a value declaration named segment, and a method named name. The representation of our class as a tree contains all the entities that we defined. And these together construct our program. A tree that we got using the method showRaw(tree) give us the skeleton of the program we wrote. The tree has String literals like SUV and Q7, value definitions like segment, and other meaningful constructs. We'll learn the basics of these literals and types of data in Scala in this chapter.
- 從零開始:數(shù)字圖像處理的編程基礎(chǔ)與應(yīng)用
- Python數(shù)據(jù)分析基礎(chǔ)
- Software Testing using Visual Studio 2012
- 云原生Spring實(shí)戰(zhàn)
- 微信公眾平臺開發(fā):從零基礎(chǔ)到ThinkPHP5高性能框架實(shí)踐
- Python機(jī)器學(xué)習(xí)編程與實(shí)戰(zhàn)
- Instant Lucene.NET
- Programming with CodeIgniterMVC
- C#程序設(shè)計(jì)(項(xiàng)目教學(xué)版)
- Mockito Essentials
- ASP.NET Core and Angular 2
- 計(jì)算機(jī)軟件項(xiàng)目實(shí)訓(xùn)指導(dǎo)
- Scratch少兒編程高手的7個(gè)好習(xí)慣
- Practical Time Series Analysis
- Building Microservices with .NET Core 2.0(Second Edition)