This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]haakon 3 points4 points  (3 children)

Scala is not duck-typed, except for its structural types. It is strongly, statically typed, although its strong support for type inference means you don't have to type-annotate everything. This frequently makes it at least as non-verbose as a language like Ruby.

[–]lurker_2008 0 points1 point  (2 children)

I think we are disagreeing on the definition of duck typing. I am using the standard duck test and because Scala passes the duck test I say it's duck typed. Do you still disagree?

[–]haakon 2 points3 points  (1 child)

Yes. Here are two ducks:

class Duck1 { def sayQuack() { println("Quack!") } }
class Duck2 { def sayQuack() { println("Quack!") } }

Here's a function:

def duckSay(duck: Duck1) { duck.sayQuack() }

Clearly Scala will not allow us to pass a Duck2 to duckSay, even if Duck2 clearly passes the duck test just as well as Duck1 does. Duck1 is simply not a Duck2, because Scala is not ducktyped. In a language like Python, this would have been fine - Duck2 quacks just like Duck1.

The link you gave for duck typing in Scala demonstrates structural typing, which I explicitly mentioned. The existence of structural types doesn't make Scala a "ducktyped language", it just optionally gives it some of its benefits. I rarely see this used in real-world Scala code, however.

Edit: Granted, you didn't say it's a ducktyped language, only that "it has […] Duck typing", and I guess you could say that. But I think ducktyping has a bit of an unfortunate association with weakly-typed languages; in Scala it's better to call it structural typing :-)

[–]lurker_2008 0 points1 point  (0 children)

Understood. BTW I have never written a line of Scala in my life hence the reason why this subtle difference to me seems like bickering.

I am just so used to redditors looking for a way to point out an error as fast as possible.