Linearization Steps - Query Related to Exam Solution Method
Moderatoren: pmueller, SE - Design and Construction
- AizazZaidee
- BASIC-Programmierer
- Beiträge: 106
- Registriert: 20. Apr 2016 22:49
Linearization Steps - Query Related to Exam Solution Method
trait A
class B
trait C extends B with A
trait D extends B
trait E extends D with A with C
class F extends D with E
Linearization of Class F: (F, E, C, A, D, B, AnyRef, Any)
Any <- AnyRef <- B <- D <- A <- C <- E <- F
and super call stack will be: F-> E -> C-> A -> D -> B -> D -> A -> C -> E - F
Is this method acceptable or do we have to follow the method mentioned in the slides. like {F, lin(E), lin(D)} etc because this is too much time and not possible to solve without making a mistake within 6 minutes ( I suppose 1 mark = 1 minute)
Thanks.
class B
trait C extends B with A
trait D extends B
trait E extends D with A with C
class F extends D with E
Linearization of Class F: (F, E, C, A, D, B, AnyRef, Any)
Any <- AnyRef <- B <- D <- A <- C <- E <- F
and super call stack will be: F-> E -> C-> A -> D -> B -> D -> A -> C -> E - F
Is this method acceptable or do we have to follow the method mentioned in the slides. like {F, lin(E), lin(D)} etc because this is too much time and not possible to solve without making a mistake within 6 minutes ( I suppose 1 mark = 1 minute)
Thanks.
Re: Linearization Steps - Related to Exam
Hi,
In general you should provide the linearization with the left side beeing the lowest type in the hierarchy, i.e. the inverse of what you have for F (I did not check whether you linearization is correct).
In the exam you should do it in the way the Task tells you to, and without intermediate steps, you most liikely will not receive all points.
Best,
Patrick
I'm not sure what you mean here.AizazZaidee hat geschrieben: ↑11. Jul 2018 14:10and super call stack will be: F-> E -> C-> A -> D -> B -> D -> A -> C -> E - F
In general you should provide the linearization with the left side beeing the lowest type in the hierarchy, i.e. the inverse of what you have for F (I did not check whether you linearization is correct).
In the exam you should do it in the way the Task tells you to, and without intermediate steps, you most liikely will not receive all points.
Best,
Patrick
Re: Linearization Steps - Query Related to Exam Solution Method
I think the correct linearization (including intermediate steps) should be the following:
Given:
trait A
class B
trait C extends B with A
trait D extends B
trait E extends D with A with C
class F extends D with E
Question:
Lin(F) = ??
Steps:
Lin(A) = {A}
Lin(B) = {B}
Lin(C) = {C, Lin(A)} >> Lin(B) = {C, A} >> {B} == {C, A, B}
Lin(D) = {D, Lin(B)} = {D, B}
Lin(E) = {E, Lin(C)} >> Lin(A) >> Lin(D) = {E, C, A, B} >> {A} >> {D, B} = {E, C, A, D, B}
Lin(F) = {F, Lin(E)} >> Lin(D) = {F, E, C, A, D, B} >> {D, B} = {F, E, C, A, D, B}
Given:
trait A
class B
trait C extends B with A
trait D extends B
trait E extends D with A with C
class F extends D with E
Question:
Lin(F) = ??
Steps:
Lin(A) = {A}
Lin(B) = {B}
Lin(C) = {C, Lin(A)} >> Lin(B) = {C, A} >> {B} == {C, A, B}
Lin(D) = {D, Lin(B)} = {D, B}
Lin(E) = {E, Lin(C)} >> Lin(A) >> Lin(D) = {E, C, A, B} >> {A} >> {D, B} = {E, C, A, D, B}
Lin(F) = {F, Lin(E)} >> Lin(D) = {F, E, C, A, D, B} >> {D, B} = {F, E, C, A, D, B}
Re: Linearization Steps - Query Related to Exam Solution Method
Hi,
on first glance this seems correct. You are missing AnyRef and Any, though.
If you want to check your results (e.g. for other examples) you can leverage the scala compiler:
Best,
Patrick
on first glance this seems correct. You are missing AnyRef and Any, though.
If you want to check your results (e.g. for other examples) you can leverage the scala compiler:
Code: Alles auswählen
import scala.reflect.runtime.universe._
class A
println(typeOf[A].baseClasses) // will print the base classes in the correct order
Patrick
Re: Linearization Steps - Query Related to Exam Solution Method
Thanks for your feedback.
However I don't quite understand why AnyRef and Any need to be mentioned explicitly in this case.
The example to the task (Topic 1 task c from the old exam - example taken directly from the slides I believe) had one class extending AnyRef and the linearization stopped at AnyRef - so I would assume that with the classes and traits from the task we would not need to mention AnyRef and Any at all... Could you please clarify that?
Edit: This is just related to correctly answering such questions in the exam - I am aware of the position of AnyRef and Any in the scala type hierarchy
However I don't quite understand why AnyRef and Any need to be mentioned explicitly in this case.
The example to the task (Topic 1 task c from the old exam - example taken directly from the slides I believe) had one class extending AnyRef and the linearization stopped at AnyRef - so I would assume that with the classes and traits from the task we would not need to mention AnyRef and Any at all... Could you please clarify that?
Edit: This is just related to correctly answering such questions in the exam - I am aware of the position of AnyRef and Any in the scala type hierarchy
Re: Linearization Steps - Query Related to Exam Solution Method
Hi,
to be correct, both AnyRef and Any are part of the type hierarchy resulting from linearization, therefore if the task in the exam does not say otherwise, you have to include it.
Best,
Patrick
to be correct, both AnyRef and Any are part of the type hierarchy resulting from linearization, therefore if the task in the exam does not say otherwise, you have to include it.
Best,
Patrick
Re: Linearization Steps - Query Related to Exam Solution Method
Code: Alles auswählen
class Any
class AnyRef extends Any
trait A extends AnyRef
class B extends AnyRef
trait C extends B with A
trait D extends B
trait E extends D with A with C
class F extends D with E
I think I didn't get it, maybe someone can help.
I understand the result "{F, E, C, A, D, B, AnyRef, Any}" but for the intermediate steps I need to know where to add AnyRef.
The SCALA type hierarchy tells me that AnyRef extends Any. But do I need the "extend AnyRef" only for class B and trait A or also for the other trait's and class F?
Thanks for any help.
Udo
Re: Linearization Steps - Query Related to Exam Solution Method
Hello,
if you did the exercise for linearization or check its solution it should get clear. Every resolution from Lin to LinResult yields the result and appends AnyRef, Any. Lin("A") === LinResult("A","AnyRef","Any"). This is true for any calculation regarding other inputs than A. When computing the result of >> alot of AnyRef and Any vanish since going by the rules for computation >> you drop AnyRef, Any on the left side when its an element of the right side. Since this is true for any LinResult it resembles rather tedious paperwork. I hope this helps.
if you did the exercise for linearization or check its solution it should get clear. Every resolution from Lin to LinResult yields the result and appends AnyRef, Any. Lin("A") === LinResult("A","AnyRef","Any"). This is true for any calculation regarding other inputs than A. When computing the result of >> alot of AnyRef and Any vanish since going by the rules for computation >> you drop AnyRef, Any on the left side when its an element of the right side. Since this is true for any LinResult it resembles rather tedious paperwork. I hope this helps.
Re: Linearization Steps - Query Related to Exam Solution Method
In principle, everything from A to F also extends AnyRef and Any, so you would have to add it to each intermediate linearization result. For example:
But the concatination removes it from the first half either way, it will always be at the end:
So as long as you always put it at the end, there is not too much to worry about.
Edit: llllllll was faster.
Code: Alles auswählen
Lin(A) = {A, AnyRef, Any}
Code: Alles auswählen
Lin(C) = {C, Lin(A) >> Lin(B)} = {C, A, B, AnyRef, Any}
Edit: llllllll was faster.

Re: Linearization Steps - Query Related to Exam Solution Method
Thank you both for your reply.
But I think you did not get my question, which linearization has really AnyRef in it?
So in the last case no ">> Lin(AnyRef)" because it comes in a later step from A respectively B? Or ist it "trait C extends B with A with AnyRef":
Because every class extends AnyRef and we have multiple inheritance.
The result would be the same, but the intermediate steps are different.
Edit:
Ok the solution of the Ex01 tells me Lin(C) = {C, Lin(A) >> Lin(B)} is correct.
Hope I got it now! Thanks for your help.
Only classes and traits, which explicit extends nothing have AnyRef in the linearization.
But I think you did not get my question, which linearization has really AnyRef in it?
Code: Alles auswählen
Lin(A) = {A, Lin(AnyRef)}
Lin(B) = {B, Lin(AnyRef)}
Lin(C) = {C, Lin(A) >> Lin(B)} <----- this step
Code: Alles auswählen
Lin(C) = {C, Lin(A) >> Lin(B) >> Lin(AnyRef)}
The result would be the same, but the intermediate steps are different.
Edit:
Ok the solution of the Ex01 tells me Lin(C) = {C, Lin(A) >> Lin(B)} is correct.
Hope I got it now! Thanks for your help.

Only classes and traits, which explicit extends nothing have AnyRef in the linearization.
Re: Linearization Steps - Query Related to Exam Solution Method
Code: Alles auswählen
Lin(AnyRef) = {AnyRef, Any}
Code: Alles auswählen
Lin(A) = {A}
Re: Linearization Steps - Query Related to Exam Solution Method
As I already said, not for the end result, but for the intermediate steps that are relevant for the exam, that was the intention of the question.
The intermediate steps are NOT the same!
Code: Alles auswählen
Lin(C) = {C, Lin(A) >> Lin(B)}
Code: Alles auswählen
Lin(C) = {C, Lin(A) >> Lin(B) >> Lin(AnyRef)}
Re: Linearization Steps - Query Related to Exam Solution Method
But you already showed that they are the same..
Code: Alles auswählen
Lin(C) = {C, Lin(A) >> Lin(B)} = {C, A, B, AnyRef, Any} = {C, Lin(A) >> Lin(B) >> Lin(AnyRef)} = Lin(C)
Code: Alles auswählen
{C, Lin(AnyRef) >> Lin(A) >> Lin(AnyRef) >> Lin(B) >> Lin(AnyRef)}
Re: Linearization Steps - Query Related to Exam Solution Method
The second line is probably not what you want, since AnyRef is usually not mixed-in like other classes. Because class C would have to look like this:UdoWeber hat geschrieben: ↑15. Jul 2018 00:20!=Code: Alles auswählen
Lin(C) = {C, Lin(A) >> Lin(B)}
Code: Alles auswählen
Lin(C) = {C, Lin(A) >> Lin(B) >> Lin(AnyRef)}
Code: Alles auswählen
class C extends AnyRef with B with A
Re: Linearization Steps - Query Related to Exam Solution Method
If you do the short version you are right, but if you do the long version like in the exercise, where you really remove right side from left side step by step, the intermdiate steps are not equal!VSchü hat geschrieben: ↑15. Jul 2018 10:00But you already showed that they are the same..So you can add Lin(AnyRef) or leave it out, it really makes no difference. EvenCode: Alles auswählen
Lin(C) = {C, Lin(A) >> Lin(B)} = {C, A, B, AnyRef, Any} = {C, Lin(A) >> Lin(B) >> Lin(AnyRef)} = Lin(C)
would result in the same linearization.Code: Alles auswählen
{C, Lin(AnyRef) >> Lin(A) >> Lin(AnyRef) >> Lin(B) >> Lin(AnyRef)}