Reading — step 1 of 6
Learn
~1 min readFirst Steps
Two binding keywords:
val— immutable binding (likefinalin Java,constin JS). Idiomatic Scala usesvaleverywhere possible.var— mutable. Avoid unless you really need mutation.
val name = "Ada" // type inferred as String
val age: Int = 36 // explicit type annotation
var counter = 0 // mutable
counter += 1
Scala is statically typed but with strong inference. You almost never need to write types except on function parameters.
Common types:
Int,Long,Short,Byte— integersDouble,Float— floatsBoolean— true/falseChar,String— textUnit— likevoid(no meaningful value)Any— top type (everything is an Any)
Read a line: scala.io.StdIn.readLine(). Parse: "42".toInt, "3.14".toDouble.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…