You're completely right that interfaces/traits are preferable to structural
types, but there are occasions where they simply can't be used.
Basically, if you're getting objects from some factory outside of your
control, then it's just not possible to mix in a trait. That's where you
have no choice to use structural types. It uses reflection and it's
undoubtedly a hack, but it is at least a type-safe hack, and definitely
better than the alternatives.
As for the DRY principle, this *can* be honoured with a structural type, the
trick is to combine it with a type alias:
type Named = { def name : String }
If I knew I was working with Java beans, then I could also use that
convention.
Following the example in your article more closely:
type NamedBean = { def getName : String }
NamedBean can now be used as the parameter type for any method I want:
def printItOut(bean : NamedBean) = println(bean.getName)
Voila! No repetition required...
Structural Typing: Type Safe, Refactorable and respects the DRY principle :)
2010/9/9 Cédric Beust ♔ <[email protected]>
>
>
> On Thu, Sep 9, 2010 at 2:34 AM, B Smith-Mannschott
> <[email protected]>wrote:
>
>>
>>
>> On Thu, Sep 9, 2010 at 08:28, Casper Bang <[email protected]> wrote:
>>
>>> > I still have some hope for Fantom because I don't think it crossed that
>>> > threshold yet, but it's getting dangerously close to it.
>>>
>>> What speaks to Fantom's advantage is it's dynamic typing feature,
>>> something Scala ignores completely - in spite of various luminaries
>>> view that the static and dynamic world will inevitably merge down to
>>> opt-in semantics. Unfortunately Scala seems to run with all the
>>> attention, regardless of the bad taste in the mouth it leaves with lot
>>> of people.
>>>
>>
>> You can get much of the benefit of dynamic typing by using Scala's
>> structural typing:
>> http://codemonkeyism.com/scala-goodness-structural-typing/
>>
>> This has been part of the languge since at least 2.6 (July 2007). (3
>> years!)
>>
>
> Yup, and two years ago, I explained why I thought that both duck typing and
> structural typing were dangerous :-)
>
> Details
> here<http://beust.com/weblog/2008/02/11/structural-typing-vs-duck-typing/>
> .
>
> (note that this is something I wrote about Scala in 2008, for people who
> think that I just discovered the language)
>
> --
> Cédric
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "The Java Posse" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<javaposse%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/javaposse?hl=en.
>
--
Kevin Wright
mail / gtalk / msn : [email protected]
pulse / skype: kev.lee.wright
twitter: @thecoda
--
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.