Seite 1 von 2

### Linearization Steps - Query Related to Exam Solution Method

Verfasst: 11. Jul 2018 14:10
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.

### Re: Linearization Steps - Related to Exam

Verfasst: 11. Jul 2018 14:24
Hi,
AizazZaidee hat geschrieben:
11. Jul 2018 14:10
and super call stack will be: F-> E -> C-> A -> D -> B -> D -> A -> C -> E - F
I'm not sure what you mean here.

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

Verfasst: 11. Jul 2018 18:37
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}

### Re: Linearization Steps - Query Related to Exam Solution Method

Verfasst: 12. Jul 2018 08:20
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:

Code: Alles auswählen

``````import scala.reflect.runtime.universe._
class A
println(typeOf[A].baseClasses) // will print the base classes in the correct order
``````
Best,
Patrick

### Re: Linearization Steps - Query Related to Exam Solution Method

Verfasst: 12. Jul 2018 11:07

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

Verfasst: 12. Jul 2018 12:03
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

### Re: Linearization Steps - Query Related to Exam Solution Method

Verfasst: 14. Jul 2018 18:15

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
``````
Hi,

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

Verfasst: 14. Jul 2018 18:30
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.

### Re: Linearization Steps - Query Related to Exam Solution Method

Verfasst: 14. Jul 2018 18:34
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:

Code: Alles auswählen

``Lin(A) = {A, AnyRef, Any}``
But the concatination removes it from the first half either way, it will always be at the end:

Code: Alles auswählen

``Lin(C) = {C, Lin(A) >> Lin(B)}  = {C, A, B, AnyRef, Any}``
So as long as you always put it at the end, there is not too much to worry about.

Edit: llllllll was faster.

### Re: Linearization Steps - Query Related to Exam Solution Method

Verfasst: 14. Jul 2018 19:22

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
``````
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":

Code: Alles auswählen

``Lin(C) = {C, Lin(A) >> Lin(B) >> Lin(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.

### Re: Linearization Steps - Query Related to Exam Solution Method

Verfasst: 14. Jul 2018 19:32

Code: Alles auswählen

``Lin(AnyRef) = {AnyRef, Any} ``
which will be added either way by one of the other linearizations. So it makes no difference if you include it at that point or not, therefore you can leave it out. On the other hand

Code: Alles auswählen

``Lin(A) = {A}``
would be incomplete, therefore you have to include it there.

### Re: Linearization Steps - Query Related to Exam Solution Method

Verfasst: 15. Jul 2018 00:20
VSchü hat geschrieben:
14. Jul 2018 19:32
which will be added either way by one of the other linearizations. So it makes no difference if you include it at that point or not, therefore you can leave it out.
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

Verfasst: 15. Jul 2018 10:00
UdoWeber hat geschrieben:
15. Jul 2018 00:20
The intermediate steps are NOT the same!
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)``
So you can add Lin(AnyRef) or leave it out, it really makes no difference. Even

Code: Alles auswählen

``{C, Lin(AnyRef) >> Lin(A) >> Lin(AnyRef) >> Lin(B) >> Lin(AnyRef)}``
would result in the same linearization.

### Re: Linearization Steps - Query Related to Exam Solution Method

Verfasst: 15. Jul 2018 10:02
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)}``
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:

Code: Alles auswählen

``class C extends AnyRef with B with A``
However, since it does not look like this and probably no class will, the first line is what you want to do.

### Re: Linearization Steps - Query Related to Exam Solution Method

Verfasst: 15. Jul 2018 12:11
VSchü hat geschrieben:
15. Jul 2018 10:00
UdoWeber hat geschrieben:
15. Jul 2018 00:20
The intermediate steps are NOT the same!
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)``
So you can add Lin(AnyRef) or leave it out, it really makes no difference. Even

Code: Alles auswählen

``{C, Lin(AnyRef) >> Lin(A) >> Lin(AnyRef) >> Lin(B) >> Lin(AnyRef)}``
would result in the same linearization.
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!