Seite 1 von 1

Ex10 Task 6: "Overriding" traits does not work

Verfasst: 4. Feb 2017 16:15
von CryNickSystems
Hi,

for quite some time I am stuck with the above task.

I simply cannot get my FireAlarm trait working so I can add some logic to the CompositeLocation trait in there.

If I do this:

Code: Alles auswählen

trait FireAlarm extends SmartHome {
    trait CompositeLocation[L <: Location] extends super.CompositeLocation[L] with TLocation {
        println("Test")
    }
}
The println() statement just won't get executed.

I mixin the trait like in the task:

Code: Alles auswählen

class MySmartHome extends SimulatedSmartHome with FireAlarm
Even if I override the abstract types in FireAlarm, it simply doesn't work. Compiles without any problems, but the code inside won't get executed.

Re: Ex10 Task 6: "Overriding" traits does not work

Verfasst: 4. Feb 2017 21:45
von CryNickSystems
Okay, I fixed it by changing my design a bit.

Re: Ex10 Task 6: "Overriding" traits does not work

Verfasst: 4. Feb 2017 22:23
von Capricorn
Should the FireAlarm trait extend the SmartHome trait? Or asked differently: does it still do in your adjusted design? As far as I can imagine there needs to be some kind of "registration" of the created sensors and actuators, because at the moment all I do is "new SomeSensor" when instantiating a room and nobody knows about the sensors or actuators... or should we delegate that instance creation into a method that as well takes core of wiring the new sensor/actuator together?! :|

Re: Ex10 Task 6: "Overriding" traits does not work

Verfasst: 5. Feb 2017 14:33
von CryNickSystems
Yes, I still extend the SmartHome trait.

(And yes, I did try to implement the Stackable Trait pattern [1] for the traits FireAlarm, Lights and Heating)

[1] http://www.artima.com/scalazine/article ... ttern.html

Re: Ex10 Task 6: "Overriding" traits does not work

Verfasst: 5. Feb 2017 22:34
von andiderp
I tried it aswell but it did not work with the stackable trait pattern. Do you have any hints for me in the right direction?

Thanks
andiderp

Re: Ex10 Task 6: "Overriding" traits does not work

Verfasst: 6. Feb 2017 13:36
von ManuelWeiel
I will try to give a hint without spoiling the solution:

Try to think about what the

Code: Alles auswählen

abstract override def put(x: Int)
method from the stackable traits example would be in your case.
When do you want to run your code in e.g. FireAlarm? Probably after "constructing" your house...