|註冊 | 登入 | 全文檢索 | 排行榜|
» Languages on JVM
列印話題 寄給朋友 訂閱主題
|作者||Re:我為何鍾情於用 Scala 做為兵刃（一、二）－－靜態型別的動態語言 [Re:linuxhsu]|
Java, Ruby, Haskell
説實在, 對我個人來説, 這沒甚麼 "神奇", "驚豔".
只不過是 generic 和 type inference.
Ruby 是 Duck type; map key can be anything.
So, it makes no sense to check the key's type on "complie" time.
The way they choose is wanted flexible and powerful, instead of static type checking safety.
Java did have such static type checking and generic.
The problem you mention is because they want the key matchs once equal method returns true,
instead of forcing type matched. This is collection library design choose ("problem?") , not core language defect.
If you don't like this way, you can implement your own wrapper class to force the generic type matchs for map.get(...) method.
Compare to Haskell, in your case, the Scala type inference seems not as "good" as Haskell.
val map = Map(1 -> "一", 2 -> "二", 2.1 -> "二點一");
could try to type inference to <Float, String> or <Double, String>
instead of AnyVal for key.
It is because most of time you rely on type inference and feel Scala type inference doing a good jog then you feel Scala is the perfect language for you.
But once you have different need, you make feel other way and don't think it is doing right...
I would like my map entry as <Object, String> type.
I initialize the map with all entries like <Int, String>
val map = Map(1 -> "一", 2 -> "二", 3 -> "三");
Later, on runtime, I may fetch database and like to insert entry <String, String> to the map.
Oops, it fail. Because of the map init's type inference force it to be <Int, String> type instead of <Object, String>, which is not what I want.
So, thing can be good or bad, depend on your point of view.
Knowing it and use it correctly. That's all. No magic.
( Type inference is good, but sometime, it may just don't guess right )
btw. Ruby code 可以更簡潔, your example can rewrite on 3 lines on Ruby 1.9 ...
|2075||我為何鍾情於用 Scala 做為兵刃（一、二）－－靜態型別的動態語言||linuxhsu||8060||2011-01-15 18:29|
|1607||Re:我為何鍾情於用 Scala 做為兵刃（一、二）－－靜態型別的動態語言||T55555||112||2011-01-15 21:35|
|1511||Re:我為何鍾情於用 Scala 做為兵刃（一、二）－－靜態型別的動態語言||T55555||2261||2011-01-16 05:25|
|1865||Re:我為何鍾情於用 Scala 做為兵刃（一、二）－－靜態型別的動態語言||linuxhsu||2810||2011-01-16 10:47|
|» JWorld@TW » Languages on JVM » Scala|