This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Scala shamelessly steals my idea!
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Remember this post where I toss out the idea of "static duck typing"?
Well, they've implemented it in Scala. Here's an example from the blog of Debasish Ghosh:
class Payroll {
def makeSalarySheet(emps: List[{ def salary: Int }]) = {
(0 /: emps)(_ + _.salary)
}
}
In short, the makeSalarySheetTakes a list of objects that respond to the salary method (and must return an Int apparently).
The notation I originally came up with was this:
def foo(arg{:bar})
...
end
Where arg is an object that must respond to the bar method.
And here's what they came up with for Scala:
{ def salary: Int }
Not that dissimilar. A big difference, though, is that the "{ def salary: Int }" is some sort of anonymous type. The notion of an anonymous type is an interesting concept, and something I want to think about in more detail later.
I should also point out (courtesy of Mauricio Fernandez) that there is a library out there from Eivind Eklund that lets you do this, though I'm not a big fan of the interface.
The title of this blog post should not be taken seriously, btw.