Mind your own data — take two

Thanks to aliases implementing single field per property was not hard, but there were still limitations:

def first Int
  var _first Int; 
  get: return field; // `field` is a local alias to `_first`
  set: field = value;
end
def second Int
  var _second Int; 
  get: return field;
  set: field = value;
end

Because constructor has to reach each of those internal fields, their names have to be unique within entire class. On the other hand with uniform aliasing inside property only single internal field is supported.

I might not borrow from Ada a lot, but after reading Programming in Ada 2012 by John Barnes one idea made its way to Skila. I cannot write such constructor:

def init()
  first._first = 0;
end

Because Int type does not have _first member. But I could write:

def init()
  first'fields._first = 0;
end

Apostrophe instead of regular dot means I ask for an attribute (or meta information, or compile time information). The terminology currently is not so important, the important fact is this extra syntax allows properties to have truly internal fields, and since I always wanted to name all the fields just field (easier copy&paste) I can finally do it:

def first Int
  var field Int; 
  get: return field; 
  set: field = value;
end
def second Int
  var field Int; // same field name
  get: return field;
  set: field = value;
end
def init()
  first'fields.field = 0;
  second'fields.field = 0;
end

Did I just introduce new syntax to solve a corner case? Yes, I am aware of that — but reflection in other languages shows it should survive.

Tagged , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: