TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Does Scala as a FP Language Suffer From Its OO Syntax?

24 点作者 bertzzie超过 12 年前

5 条评论

andolanra超过 12 年前
Fundamentally, this post conflates <i>type classes</i> with <i>functional programming</i>. Yes, type classes are one way that functional languages approach ad-hoc polymorphism (i.e. distinguishing which implementation of a function to use based on the types of its arguments.) This is not the only way to approach said problem. For example, OCaml has both objects and modules/functors to support ad-hoc polymorphism. That Scala and Haskell take a different approach to this problem does not make Scala "less" functional than Haskell.[1]<p>As usual, the real problem is one of terminology. If you take FP to mean "Haskell and Clojure", then one could easily find features of, say, Erlang or OCaml or Scheme that are "not FP." All of these are conventionally considered functional languages, and IIRC none of them—save Scheme-based ports of CLOS—feature multimethods or type classes. So, before nitpicking about whether a language is more or less FP, one should always clarify: <i>what do I mean by FP?</i><p>[1]: Not on its own, anyway; there are other arguments that can be made both ways, but this is not the space to make them.
crntaylor超过 12 年前
Why the focus on type classes? Yes, type classes can sometimes be very helpful (the Num, Monad and Functor typeclasses in Haskell for example - where would we be without them?)<p>But overuse of type classes in any language is an antipattern. Luke Palmer sums it up well in his blog post [1]<p><i>Advanced type system features have their place, but plain old functions go a long, long way. Functions are the masters of reuse: when you use an advanced feature, you need a yet more advanced feature to abstract over it. But all you need to abstract over a function is another function.</i><p>Using type classes where they're not appropriate leads to 1. writing unnecessary boilerplate code, 2. torturing the type system and 3. a slippery slope of adding more and more advanced type system features to bludgeon your poor design choice into the right shape. Use functions!<p>It's also worth reading Gabriel Gonzalez's post on a similar topic [2]<p>[1] <a href="http://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/" rel="nofollow">http://lukepalmer.wordpress.com/2010/01/24/haskell-antipatte...</a><p>[2] <a href="http://www.haskellforall.com/2012/05/scrap-your-type-classes.html" rel="nofollow">http://www.haskellforall.com/2012/05/scrap-your-type-classes...</a>
评论 #4659002 未加载
eranation超过 12 年前
No. (<a href="http://en.wikipedia.org/wiki/Betteridges_Law_of_Headlines" rel="nofollow">http://en.wikipedia.org/wiki/Betteridges_Law_of_Headlines</a>)<p>But seriously, I don't feel so, (though I came from writing Java). I like the protection of static typing, and haven't encountered yet anything that made the FP part suffer from the OO part.<p>p.s. if you haven't already, Martin Odersky himself is delivering a course named Functional Programming in Scala (still not too late to join) <a href="https://class.coursera.org/progfun-2012-001" rel="nofollow">https://class.coursera.org/progfun-2012-001</a> It might be too basic if you are fluent, but I found it just in the right level for me. In any case, learning a language from it's creator seems to give you some insights.
jjm超过 12 年前
Once you understand that Scala is a hybrid, all those not-pure FP aspects start to make sense in that context.
ndepoel超过 12 年前
I think you ought to see this the other way round: Scala as an OO language definitely benefits from its FP syntax.