Skip to main content

Scala :: multiple traits mix-in

Scala allow the multiple trait mix-in.

On the Java, java also allow the multiple interface implementation. What is the difference between Scala's trait and Java's interface? There are similar thing. We use trait as interface define. But, trait can have it own field and implementation of there defined method. Java's interface cannot it own some values and implementations that the interface defined the methods.

I had a question about trait mix-in. The question is "when some of traits are defined same name of methods and the methods also have some different implements then which method will be active?"
For example,
---------------------------------------------------------
trait A { def x = println("A......x")}
trait B { def x = println("B......x")}

class C extends A with B
class D extends B with A
---------------------------------------------------------
In such case, trait A and B define the method named x. And both x on each trait have also implementation. When we want to define the class C and D what will be happened? You can see only error when you will compile it. The error meaning is we need override the method x in class C and D... On my 1st idea, scala compiler will chose one of implements by the priority. The really behavior of compiler, this idea was false. We have define the new method with "override".

So, we need to write code as following thing. When we define the class C and D, we need also to define the method x on each class with "override" keyword.
------------------------------------------------------------
trait A { def x = println("A......x")}
trait B { def x = println("B......x")}

class C extends A with B { override def x = println("C.........x")}
class D extends B with A { override def x = println("B.........x")}
------------------------------------------------------------

Post a Comment

Popular posts from this blog

SSID in japanese?

Usually, we don't use japanese for setting to computer. Because, japaese needs at least two byte. Usually, many computer systems are designed for one byte. So, to use japanese in the system setting was not common way. If he is usual engineer then he will not set japanese for the setting to SSID. I was not do such a thing. Today, i read a tweet written by my friend. He wrote that he could see the japanese ssid from his house. I was really  suprized when i read it. So, I asked prof. Google about this ssid expression in japanese. So, it is  exist.Now, I had no wireless devces for trying it. But,it is so intersting thing for me.

TokyoWalking : Tram @Arakawa-Line

End of last year, I went to cycling to Ikebukuro. Ikebukuro is one of the main city on Yamanote-line (loop line) It is north area of Tokyo (loop-line).

Basically, most rail lines on mid-tokyo are as subway or elevated railway. So, it made my so surprising, when I saw it. 
This is tram in tokyo. I did not know that tokyo also has a tram.

Following photo is another tram station.

I saw several tram on the railway. Most of them are painted with different line (maybe...) This tram is named Toden-Arakawa-Line. This line is for Waseda University. I could not take the tram, becase I rode my bicycle. However, I will take the tram on some day.

Following river is River, Kanda. The river is used on some old japanese song.


Bicycle insurance in japan

Long time, I was considering about bicycle insurance.

Many japanese are not consider about insurance for bicycle in japan. Because japan is weak country for insurance.

Many people considered about when they are attacked by car. They didn't have an idea what they may be going to be individual at fault. However, road bikes are faster than my MTB. And, many of city bikes do not turn on a head light after sunset. The old person has also no idea what the bicycles can ride so fast as 50cc motar bike. Then they easy enter in front of bicycles. Such kind of accident is not few in japan. And, the worst case of such accident, the road bike rider needed to pay 40 millions yen to attacked person.

Bicycle ride's bad manner have begun to social issues. Some bicycle riders do not follow the traffic lights or traffic signs. In such background, sometime, I have risks what I will be attacked by such bicycle riders when I ride my MTB. However, as above reason, many riders have no insurance for r…