The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

jvalverde (1) [Avatar] Offline
#1
1) I execute :


var marksSum = 0
var marksCount = 0

def averageMark: Double = marksSum.toDouble / marksCount

def mark(q1: Double, q2: Double, q3: Double): Int = {
val avgScore = (q1 + q2 + q3) / 3
val scoreInScale = avgScore * 10 / 3
val mark = Math.round(scoreInScale)

marksSum += mark
marksCount += 1
mark
}


And i get three errors :

type mismatch;
[error] found : Int
[error] required: ?{def +(x$1: ? >: Long): ?}
[error] Note that implicit conversions are not applicable because they are ambiguous:
[error] both method int2long in object Int of type (x: Int)Long
[error] and method int2float in object Int of type (x: Int)Float
[error] are possible conversion functions from Int to ?{def +(x$1: ? >: Long): ?}
[error] marksSum += mark
[error] ^
[error] /Main.scala:25:16: value += is not a member of Int
[error] Expression does not convert to assignment because:
[error] overloaded method value + with alternatives:
[error] (x: Int)Int <and>
[error] (x: Char)Int <and>
[error] (x: Short)Int <and>
[error] (x: Byte)Int
[error] cannot be applied to (Long)
[error] expansion: Main.this.marksSum = Main.this.marksSum.+(mark)
[error] marksSum += mark
[error] ^
[error] /Main.scala:27:7: type mismatch;
[error] found : Long
[error] required: Int
[error] mark
[error] ^
[error] three errors found



2) And :

Figure 4.2: Syntax diagram of how to define a variable in Scala.
A variable is an immutable assignment, you can reassign it more than once.


You ment that a variable is a mutable assignment
Daniela Sfregola (12) [Avatar] Offline
#2
Hi jvalverde,
thanks! This has been fixed in the next MEAP release.

Regards,
Daniela