Hi Guys,
I am hardware engineer and new to scala. I am working through a guid in the flowing link.
github: chisel-bootcamp/2.3_control_flow.ipynb at master · freechipsproject/chisel-bootcamp · GitHub
I am trying to solve the question about statemap with match…case…
It seems function states return with type Unit. But in the solution, nextstate is assigned to states(…). So it was Int.
I am confused. Why is that?
Codes:
// state map
def states = Map("idle" -> 0, "coding" -> 1, "writing" -> 2, "grad" -> 3)
// life is full of question marks
def gradLife (state: Int, coffee: Boolean, idea: Boolean, pressure: Boolean): Int = {
var nextState = states("idle")
// my solution below
nextState = state match {
case states("idle") => {
if (idea) states("idle")
else if (coffee) states("coding")
else if (pressure) states("writing")
}
case states("coding") => {
if (coffee) states("coding")
else if (pressure || idea) states("writing")
}
case states("writing") => {
if (coffee || idea) states("writing")
else if (pressure) states("grad")
}
case states("grad") => states("idle")
}
/////
nextState
}
Resutls:
cmd8.sc:7: not found: value states
case states("idle") => {
^cmd8.sc:10: type mismatch;
found : Unit
required: Int
else if (pressure) states("writing")
^cmd8.sc:12: not found: value states
case states("coding") => {
^cmd8.sc:14: type mismatch;
found : Unit
required: Int
else if (pressure || idea) states("writing")
^cmd8.sc:16: not found: value states
case states("writing") => {
^cmd8.sc:18: type mismatch;
found : Unit
required: Int
else if (pressure) states("grad")
^cmd8.sc:20: not found: value states
case states("grad") => states("idle")
^Compilation Failed
solutions of Guid:
if (state == states("idle")) {
if (coffee) { nextState = states("coding") }
else if (idea) { nextState = states("idle") }
else if (pressure) { nextState = states("writing") }
} else if (state == states("coding")) {
if (coffee) { nextState = states("coding") }
else if (idea || pressure) { nextState = states("writing") }
} else if (state == states("writing")) {
if (coffee || idea) { nextState = states("writing") }
else if (pressure) { nextState = states("grad") }
}