thetrystero thetrystero - 15 days ago 6
Scala Question

How to access methods in a class nested within an abstract class?

EDIT: I'm not sure why I was downvoted for this question. This is a legit question because I'm taking the online coursera course in Reactive Programming by Martin Odersky, and if you go and look at lecture 3.6 "discrete event simulation implementation and test" yourself, you will see that Martin implemented the code exactly as I did and he did not get these errors. This was not an assignment, all code was presented and I simply copied it from the slides. You can see below screenshots from the actual video showing that the gates are written inside of Wire class.

enter image description here
enter image description here

I have an abstract class

Circuits
(in file Circuits.scala) which extends an abstract class
Gates
. Within
Gates
is defined a class
Wire
(both Gates and Wire are in file Gates.scala). And inside of this class
Wire
is defined several functions one of which is
orGate
. When I try to access
orGate
and other functions inside
Wire
, the IDE complains with
symbol not found
. What special thing do I need to do so that
orGate
etc. are visible from
Circuits
? Code snippets to illustrate my issue follows.

file Circuits.scala:

package week3
abstract class Circuits extends Gates {

def halfAdder(a: Wire, b: Wire, s: Wire, c: Wire) {
val d, e = new Wire
orGate(a, b, d) // <--- symbol not found: orGate
andGate(a, b, c) // <---- symbol not found: andGate
inverter(c, e) // <--- etc.
andGate(d, e, s)
}

def fullAdder(a: Wire, b: Wire, cin: Wire, sum: Wire, cout: Wire) {
val s, c1, c2 = new Wire
halfAdder(a, cin, s, c1)
halfAdder(b, s, sum, c2)
orGate(c1, c2, cout)
}

}


file: Gates.scala:

package week3


abstract class Gates extends Simulation {

def InverterDelay: Int
def AndGateDelay: Int
def OrGateDelay: Int

class Wire {

private var sigVal = false
private var actions: List[Action] = List()

def getSignal: Boolean = sigVal
def setSignal(s: Boolean): Unit = {
if (s != sigVal) {
sigVal = s
actions foreach (_())
}
}
def addAction(a: Action): Unit = {
actions = a::actions
a()
}

def inverter(input: Wire, output: Wire): Unit = {
def invertAction(): Unit = {
val inputSig = input.getSignal
afterDelay(InverterDelay) { output setSignal !inputSig}
}
input addAction invertAction
}


def andGate(in1: Wire, in2: Wire, output: Wire): Unit = {
def andAction(): Unit = {
val in1Sig = in1.getSignal
val in2Sig = in2.getSignal
afterDelay(AndGateDelay) {output setSignal (in1Sig & in2Sig)}
}

in1 addAction andAction
in2 addAction andAction
}


def orGate(in1: Wire, in2: Wire, output: Wire): Unit = {
def orAction(): Unit = {
val in1Sig = in1.getSignal
val in2Sig = in2.getSignal
afterDelay(OrGateDelay) {output setSignal (in1Sig | in2Sig)}
}

in1 addAction orAction
in2 addAction orAction
}

def probe(name: String, wire: Wire): Unit = {
def probeAction(): Unit = {
println(name, currentTime, wire.getSignal)
}
wire addAction probeAction
}


}
}

Answer

orGate and most other things, except addAction and setSignal should be defined outside of Wire.

Also, you really writing java code here (albeit in scala syntax). I encourage you to find a book or online resource on scala and read through the first few chapters to familiarize yourself with the basic concepts and paradigms of the language.

Comments