On 2013-06-06 10:45, Chris Angelico wrote:

For the "accept any object that has a next() method" sorts of rules, I
don't know of any really viable system that does that usefully. The
concept of implementing interfaces in Java comes close, but the class
author has to declare that it's implementing some named interface. In
theory there could be something that deduces the validity from the
given structure, but I'm not aware of any language that does this. But
it would let you do stuff like this (prototyped in Python):

class Integers:
     def __init__(self): self.value=0
     def next(self):
         self.value+=1
         return self.value

interface Iterable:
     next(self)

def grab_three_values(Iterable iter):
     return iter.next(),iter.next(),iter.next()

With a language that checks these sorts of things at compile time,
it's not a big deal to test. With something fully dynamic like Python,
it's probably not worth the effort. But maybe checks like this could
be useful to something like Coverity.

As Serhiy notes, Go does this, almost exactly as you wrote it (modulo syntax).

http://golang.org/doc/effective_go.html#interfaces_and_types

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to