It has been a recurrent theme on the mailing list, to request access
to various behaviors that are so far only accessible to certain
builtin objects.
There are a couple of capabilities that I would like to make available
for any type:
* Iteration with range
* length
* Being keys in a map -- discussion:
http://groups.google.com/group/golang-nuts/browse_thread/thread/b91933187bd51d22/dc64823a6f4841d4
Even operator overloading falls inside this model, but should be
judged separately.
Each of these capabilities can be specified by an interface that can
be checked statically, and that the builtin types are implicitly
assumed to implement.
Iteration with range could be expressed as:
type Iterable interface {
Iter() <-chan interface{}
}
(this is seen in exp/iterable)
Should we allow "special interfaces" for these builtin capabilities,
OR generics are allowed into the language, we could formulate the
interface looser:
type Iterable interface {
Iter() <-chan T
}
Where T is any type. Now, any type implementing the Iterable interface
can be iterated with a range expression. The name Iterable would also
be available as a builtin interface name.
For the builtin len() function one can similarly suggest:
type Sized interface {
// Len is the number of elements in the collection.
Len() int
}
And for keys in a the builtin hash map, it was suggested in a
neighboring thread:
type Hashable interface {
Hash() int;
Equals() bool;
}
In general, I would like Go to open up this way. Less special cases,
more equality between builtin types and custom types.
Ulrik