페이지

2015년 11월 3일 화요일

Scala2e Chapter5 Basic Types and Operations 5.2 Literals

All of the basic types can be written with literals. A literal is a way to write a constant value directly in code.

- Integer literals
Int, Long, Short, Byte
forms: decimal, hexadecimal, octal

scala> val hex = 0x5
hex: Int = 5
scala> val hex2 = 0x00FF
hex2: Int = 255
scala> val magic = 0xcafebabe
magic: Int = -889275714

scala> val oct = 035  // (35 octal is 29 decimal)
oct: Int = 29
scala> val nov = 0777
nov: Int = 511
scala> val dec = 0321
dec: Int = 209

scala> val dec1 = 31
dec1: Int = 31
scala> val dec2 = 255
dec2: Int = 255
scala> val dec3 = 20
dec3: Int = 20
scala> val prog = 0XCAFEBABEL
prog: Long = 3405691582
scala> val tower = 35L
tower: Long = 35
scala> val of = 31l
of: Long = 31

- Floating point literals

Floating point literals are made up of decimal digits, optionally containing a decimal point, and optionally followed by an E or e and an exponent.

If a floating-point literal ends in an F or f, it is a Float, otherwise it is a Double.
Optionally, a Double floating-point literal can end in D or d.

scala> val big = 1.2345
big: Double = 1.2345
scala> val bigger = 1.2345e1
bigger: Double = 12.345
scala> val biggerStill = 123E45
biggerStill: Double = 1.23E47
scala> val little = 1.2345F
little: Float = 1.2345
scala> val littleBigger = 3e5f
littleBigger: Float = 300000.0
scala> val anotherDouble = 3e5
anotherDouble: Double = 300000.0
scala> val yetAnother = 3e5D
yetAnother: Double = 300000.0

- Character literals
Character literals are composed of any Unicode character between single quotes, such as:

scala> val a = 'A'
a: Char = A

The octal number must be between '\0' and '\377'.

A character literal can also be given as a general Unicode character consisting
of four hex digits and preceded by a \u.


scala> val c = '\101'
c: Char = A

scala> val d = '\u0041'
d: Char = A

scala> val f = '\u0044'
f: Char = D



In fact, such Unicode characters can appear anywhere in a Scala program.

scala> val B\u0041\u0044 = 1
BAD: Int = 1

scala> val backslash = '\\'
backslash: Char = \


- String literals
A string literal is composed of characters surrounded by double quotes

Scala includes a special syntax for raw strings. You start and end a raw string with three double quotation marks in a row(""").

println("""Welcome to Ultamix 3000.
Type "HELP" for help.""")


Welcome to Ultamix 3000.
                 Type "HELP" for help.

The isssue is that the leading spaces before the second line are included in the string!.
To help with this common situation, you can call stripMargin on strings.
To use this method, put a pipe character(|) at the front of each line, and then call stripMargin on the whole string

println("""|Welcome to Ultamix 3000.
|Type "HELP" for help.""".stripMargin)


Welcome to Ultamix 3000.
Type "HELP" for help.


- Symbol literals
A symbol lilteral is written 'ident, where ident can be any alphanumeric identifier.
Symbol literals are typically used in situations where you would use just an identifier in a dynamically typed language. For instance, you might want to define a method that updates a record in a database;

scala> def updateRecordByName(r: Symbol, value: Any){
//code goes here
}
updateRecordByName: (Symbol, Any)Unit

The method takes as parameters a symbol indicating the name of a record field and a value with which the field should b updated in the record.
In a dynamically typed language, you could invoke this operation passing an undeclared field identifier to the method, but in Scala this would not compile:

scala> updateRecordByName(favoriteAlbum, "OK Computer")
:6: error: not found: value favoriteAlbum
           updateRecordByName(favoriteAlbum, "OK Computer")

Instead, and almost as concisely, you can pas a symbol literal:

scala> updateRecordByName('favoriteAlub, "OK Computer")

There is not much you can do with a symbol, except find out its name:

scala>val s = 'aSymbol
s: Symbol = 'aSymbol

scala>s.name
res20: String = aSymbol

Another thing that's noteworthy is that symbols are interned. If you write the same symbol literal twice, both expressions wil refer to the exact same Symbol object.

- Boolean Iiterals
The Boolean type has two literals, true and false:














댓글 없음: