JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Languages on JVM » Scala  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
作者 Re:我為何鍾情於用 Scala 做為兵刃(一、二)--靜態型別的動態語言 [Re:linuxhsu]

Java, Ruby, Haskell


發文: 1026
積分: 24
於 2011-01-16 05:25 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
linuxhsu wrote:
如何,是不是很神奇呢?這也是我喜歡上 Scala 的一個原因
那麼,Scala 又是如何呢?事實上,讓我對 Scala 感到驚豔的地方,就在於他

説實在, 對我個人來説, 這沒甚麼 "神奇", "驚豔".
只不過是 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...

For example:
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 ... Wink
@zipCode = {221 => "汐止", 115 => "南港", 545 => "埔里"}
getArea221 = ->(shouldCrash) { @zipCode[ shouldCrash ? "abc" : 221 ] }
puts("Hello World\n#{getArea221.call(false)}\n#{getArea221.call(true)}\n")

T55555 edited on 2011-01-16 05:49
reply to postreply to post
人氣 標題 作者 字數 發文時間
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

reply to postflat modego to previous topicgo to next topic
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8