Reputation: 1337
If you were trying to explain someone how nicely Scala blends functional and object-oriented techniques, which example would you use?
Upvotes: 6
Views: 362
Reputation: 7979
Martin has pointed to PartialFunction
as a signal example of the kind of thing that the OO/FP synthesis uniquely enables. Specifically, you can treat it as a function and just call it, running the risk of an exception, or you can ask it first whether it's likely to throw given some argument. The former is a natural consequence of first-class functions, and you'd expect to see it in any self-styled functional language; the latter is arguably something special.
Upvotes: 1
Reputation: 2151
In my canned Seductions of Scala talk, I end with an Actor example that uses functional-style pattern matching for determining the "kind" of message received and object-oriented-style polymorphic dispatch for one of the message "kinds" (a geometric shape to draw). It starts around slide 76.
Upvotes: 4
Reputation: 49705
The actor API is a great example of how the strengths of both approaches are used together. You can also look at the implementation of Map, and the way that it subclasses Function1
Upvotes: 1