Exactly.  The Camera/Gun argument against structural typing doesn't hold
water because the programmer's error is exactly the same semantic problem as


// my library
interface Camera {
   void shoot();
}

// somebody else's code
class Bazooka extends Camera {...}

The only constraints that Java can enforce on implementing the Camera
interface are structural, it knows nothing about our domain.  In this
particular case the mistake seems silly because of our intuition that
Bazookas aren't Cameras.  But Java also allows us to say

// my library
// this interface is for things that "shoot" non-destructively like still
cameras, video cameras, and sextants.
interface Shootable {
  void shoot();
}

Now writing "class Bazooka extends Shootable {...}" violates the library's
constraint, but the constraint is in documentation that is easily missed by
a programmer.  In this toy domain that seems contrived, but real world
examples abound where classes implement the structural requirements of an
interface but don't follow the additional requirements in the documentation.

On Thu, Aug 20, 2009 at 2:26 AM, Ben Schulz <[email protected]> wrote:

>
> > should that be legal code? I'd argue no - the fact that both Gun and
> > Camera have a 'shoot' method is a pure coincidence.
>
> I don't think that argument works here. Yeah, idiots will be idiots,
> but it would take an extraordinary idiot to call shoot() on an object
> of type "Gun|Camera". Why do you insist on having more slack in your
> type system, just to exclude a tiny class of errors that noone is
> going to make anyways?
>
> With kind regards
> Ben
> >
>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to