← Back to team overview

ooc-dev team mailing list archive

issue with properties?


I began playing around with ooc a day or two ago. I tried writing something
like this:

Fraction : class {

  numerator: Int {
    set(n) {
      numerator = n
      this normalize()

  denominator: Int {
    set(d) {
      if(d != 0) {
        denominator = d;
        this normalize()

  init: func (=numerator,=denominator){}

  normalize: func {
    "normalize()" println()
    gcd := this gcd()
    if(gcd != 0) {
      numerator /= gcd
      denominator /= gcd

  gcd: func -> Int {
    (a,b) := (this numerator, this denominator)
    while(b != 0) {
      t := b
      b = a % b
      a = t

main: func {
  f := Fraction new(4,5)
  f denominator = 8
  "%d/%d" printfln(f numerator,f denominator)

I thought this would work, but then I realized that it resulted in an
infinite recursion. Since normalize assigns to numerator and denominator,
this invokes the property setter which in turn runs normalize().

So, a) it would seem that the way to implement something like this would be
to have actual getters and setters for numerator and denominator (instead of
properties)? Is there a better way to do this?
And, b) (this may be irrelevant depending on the answer to a) might it be
useful to add some way of allowing methods to bypass properties that may
exist and access instance data directly?

Follow ups