Scala tips and code snippets.
A literal is a raw value that are directly written in a source code.
Ex: 3.14
, "Hi!"
, false
, etc…
There are two representations of integer literals in Scala:
0
, 3615
, -99
, etc…0x0
, 0xe1f
, -0x63
, etc…The character l
or L
can be appended to an integer literal to specify
that its type is Long
. Ex: 9016651116L
, -0x2196F2D6Cl
Example:
// Sum integer values.
val x = 1234
val y = -0x4d2 // 0x4d2 is the hexadecimal representation of 1234
println(y)
// --> 1234
val sum = x + y
println(s"the sum is: ${ sum }")
// --> the sum is: 0
Real number are approximately represented by floating point numbers.
There are two notations for floating point numbers:
0.0
, 36.15
, -.99
(same as -0.99)0e0
, .3615e2
, -9.9e-1
, etc…Dy default the floating point literals are instances of Double
type.
But it is possible to choose the literal type by appending:
f
or F
to create a Float
value: 0.0f
, .3615e2F
, -.99f
…d
or D
to create a Double
value: 0.0d
, .3615e2D
, -.99d
…// Printing floating point numbers.
val pi = 31415e-4d
println(pi)
// --> 3.1415
val onePercent = .01
println(s"the percentage is ${ 100 * onePercent }%")
// --> the percentage is 1.0%
There are two Boolean literals: true
and false
// Comparing boolean numbers.
val isOK = true
val isKO = false
val isStillOK = isOK != isKO
println(s"that's still OK? $isStillOK")
// --> that's still OK? true
In Scala, character literals are written in simple quotes '
and there are two way to represent a character literal:
'a'
, 'b'
, '@'
, '5'
, '☺'
…'\n'
, '\t'
, '\u0020'
// Print special characters.
val asciiT = 'T'
val unicodeTopArrow = '\u2191'
println(s"go to the ${ asciiT }op $unicodeTopArrow")
// --> go to the Top ↑
String literals are written in double quotes "
or triple double quotes """
.
Single line string are written in double quotes: "hello"
.
A double quote char that is inside a single line string must be escaped \"
.
Ex: "{\"json_value\": 4}"
.
There are over characters that can be escaped too:
Multi-lines string are written in triple double quotes:
"""C:\path\to"""
.
Unicode characters can still be escaped in multi-line strings,
but the other escape sequences above are not interpreted.
The sequences are represented as they are written.
// Conveyance.
// newline character is escaped in the following single line string
val textPart1 = "Car\nMotorbike"
// no need to escape the newline characters in this multi-lines string
val textPart2 = """
Bike
Plane
"""
println(textPart1 + textPart2)
// --> Car
// --> Motorbike
// --> Bike
// --> Plane
String literals can be represented with an interpolation indicator:
s"${variableName}"
: replaces the variable name by its value.f"${variableName}%d"
: replaces the variable name by its value
and applies the specified formatting.raw"unescaped\text"
: the escape characters are not interpreted.val pi = 3.1415
println(s"value: $pi")
// --> value: 3.1415
println(f"value: $pi%.1f")
// --> value: 3.1
println(raw"\new look")
// --> \new look
A symbols is a kind-of string that can be used by the application logic,
while actual String
values are used as application data.
A symbol literal is represented by a word starting with a simple quote '
:
'connectionStarted
// Display exit cause.
val statusCodes = Map('exitSuccess -> 0, 'exitFailure -> 1)
val code = statusCodes('exitSuccess)
println(s"the status code is $code")
// --> the status code is 0
An XML literal represents an XML content that is written inline in the source code:
// Find a link in HTML content.
val htmlDiv =
<div>
<a href="https://example.com">
Click here
</a>
</div>
val link = (htmlDiv \\ "@href").text
println(s"the extracted link is $link")
// --> the extracted link is https://example.com
This project is maintained by
Y. Somda (yoeo)
Logo by
Icon Island
— theme by
orderedlist
— remixed by
yoeo