Advanced Computing Environment
Hosted by SourceForge
brix-os project page

Previous: Booleans ----- Up: Contents ----- Next: Ranges


show notes
Features that need to be added to the page or ideas that haven't been thought out.
  • the iterator returns an object value which is required for Ranges but some types would be better returning references. EACH has the option to make elements mutable and the iterator should support this as well. three different functions could be used to create iterators: makeIterator(), makeRefIterator() and makeMutableIterator(). mutable EACH could start by calling the mutable function which throws and exception for Range and EACH will rethrow this error. if mutable elements aren't needed EACH can test the object size and use either the reference iterator or the value iterator. if it selects reference and the collection doesn't support references then makeRefIterator will throw an error and EACH would fallback to makeIterator(). this all happens at compile-time and has no performance impact on the runtime.
  • an IN infix operator could return true if an element is found within a collection
    	if("bar" in "foobarbaz")
  • slice type and operator, contains reference to the collection along with the upper and lower bounds of the slice.

The collection_interface is used by the EACH extension and should be implemented by any type that represents a set of physical or virtual elements.

	// interfaces
	collection_iterator_interface = deftype (T:Type) {
	  method reset();
	  method getCurrent() -> T;
	  method moveNext() -> (T, end_of_collection);

	collection_interface = deftype () {
	  `parameter T:Type = _
	  makeIterator = def (`ref r:Self, reversed:Boolean)
	    -> collection_iterator_interface(Self.T);
	  isEmpty = def (o:Self) -> Boolean;

	// macro
	`export-root empty? = defx`open-macro (right:collection_interface)
	  -> (typeOf right).isEmpty(right)

Types implementing this interface should also define shadow methods for collection_iterator_interface. This lets EACH generate optimized code instead of dispatching methods but is not required.

Previous: Booleans ----- Up: Contents ----- Next: Ranges