Advanced Computing Environment
Hosted by SourceForge
brix-os project page

Previous: Destructors ----- Up: Contents ----- Next: Compound Accessors


show notes
Features that need to be added to the page or ideas that haven't been thought out.
  • in their current form, accessors are only intended to map small structures to tuples and vice versa. they could however be used to map a collection to a tuple but would need an efficient method of destructuring the collection and also a syntax for the getter.
    	// the ... parameter enables generator code
    	getter (...) -> generator code
    	(x, _, y, _) := some_collection
    the internal tuple representation can invoke the generator as easily as it iterates over a stack of rvalue expressions.
  • auto slots will have a constructor, setter and getter automatically declared
    	Foo = deftype (T:Type) `auto slot a:T = _
    	Foo = deftype (T:Type) `auto slot (a:T, b:T) = _
    only one auto slot declaration allowed and non-tuple declarations won't have a getter defined.

Destructuring and Reconstructing
The constructor defines how to initialize the object with a tuple of values and by default the object is passed around as a blob and methods invoked on it. The assignment operator has a destructuring mode that allows it to assign multiple lvalues if the rvalue has a destructuring interface. Setters take a parameter list and evaluate an expression when a value or tuple is assigned to the object. The getter expression is returned when destructuring the object into components. A getter expression must be a tuple of values and can never be a single value.

SETTER context (parameters) -> X-body
GETTER context (values)
  • context -- optional context-oriented flag
  • parameters -- parameter list for setters
  • values -- list of return values for getters
  • -> -- required syntax for setters
  • X-body -- single or block expression for setters
	Point := deftype () {
	  constructor (x, y)
	  constructor `polar (r, a) -> `private (x, y) = (r*cos(a), r*sin(a))
	  slot (x:Int, y:Int) = (x, y)
	  setter (x, y) -> (self.x, self.y) = (x, y)
	  getter (self.x, self.y)
	  setter `polar (r, a) -> (self.x, self.y) = (r*cos(a), r*sin(a))
	  getter `polar (sqrt(self.x^2+self.y^2), atan(self.y/self.x))
	p1:Point = (0,0) // constructor
	(x,y) := p1 // destructuring
	p1 = (1,2) // reconstruct

	p1 = `polar (1.414, 45) // reconstruct with polar coordinates
	(r, a) := `polar p1 // polar destructuring

	p2 := p1 // p1 is destructured and p2 is constructed
	p3 := `ref p1 // p3 is a reference to p1

Previous: Destructors ----- Up: Contents ----- Next: Compound Accessors