scala-primary-constructor


Scala local variable inside primary constructor

梦想与她 提交于 2019-12-13 12:32:05
问题 How in Scala I can define local variable in primary constructor? I need to solve this exercise from Scala for the impatient book: Write a class Person with a primary constructor that accepts a string containing a first name, a space, and a last name, such as new Person("Fred Smith"). Supply read-only properties firstName and lastName. Should the primary constructor parameter be a var, a val, or a plain parameter? Why? And for now my solution looks like this: class Person(firstLast: String) {

Scala local variable inside primary constructor

本小妞迷上赌 提交于 2019-12-06 02:16:39
How in Scala I can define local variable in primary constructor? I need to solve this exercise from Scala for the impatient book: Write a class Person with a primary constructor that accepts a string containing a first name, a space, and a last name, such as new Person("Fred Smith"). Supply read-only properties firstName and lastName. Should the primary constructor parameter be a var, a val, or a plain parameter? Why? And for now my solution looks like this: class Person(firstLast: String) { private[this] val firstLastAsArr = firstLast.trim.split(" ") val firstName = firstLastAsArr (0) val

How do you define a local var/val in the primary constructor in Scala?

纵饮孤独 提交于 2019-11-26 15:43:39
问题 In Scala, a class's primary constructor has no explicit body, but is defined implicitly from the class body. How, then, does one distinguish between fields and local values (i.e. values local to the constructor method)? For example, take the following code snippet, a modified form of some sample code from "Programming in Scala": class R(n: Int, d: Int) { private val g = myfunc val x = n / g val y = d / g } My understanding is that this will generate a class with three fields: a private "g",

Do scala constructor parameters default to private val?

♀尐吖头ヾ 提交于 2019-11-26 14:10:31
I have been trying: class Foo(bar: Int) vs: class Foo(private val bar: Int) and they seem to behave the same although I couldn't find anywhere saying that (bar: Int) expands to (private val bar: Int) so my question is, are these identical/similar? On a side note, I have been trying to use -Xprint:typer on these code pieces and they produce the same code except for an extra line in the second one. How do I read that extra line? .. class Foo extends scala.AnyRef { <paramaccessor> private[this] val bar: Int = _; def <init>(bar: Int): this.Foo = { Foo.super.<init>(); () } } .. .. class Foo extends

Do scala constructor parameters default to private val?

偶尔善良 提交于 2019-11-26 03:49:13
问题 I have been trying: class Foo(bar: Int) vs: class Foo(private val bar: Int) and they seem to behave the same although I couldn\'t find anywhere saying that (bar: Int) expands to (private val bar: Int) so my question is, are these identical/similar? On a side note, I have been trying to use -Xprint:typer on these code pieces and they produce the same code except for an extra line in the second one. How do I read that extra line? .. class Foo extends scala.AnyRef { <paramaccessor> private[this]

工具导航Map