Updated getter/setter syntax?

What are your thoughts about an updated syntax for writing getters/setters?

.NET style?

class MyClass {
    public Number TheAnswerToLifeTheUniverseAndEverything {
        get() { return 42; }
        set(Number answer) {
            if (answer == 42)
                return answer;  
            else 
                throw(message="Wrong answer");
        }
    }

    public Number MagicNumber {
        set(Number num) {
            // whatever is returned from set() is what is put into this.MagicNumber
            return num*2;
        }
    }
}

ES6 style?

class MyClass {
    get TheAnswerToLifeTheUniverseAndEverything() {
        return 42;
    }

    set TheAnswerToLifeTheUniverseAndEverything(Number answer) {
        if (answer == 42)
            return answer;  
        else 
            throw(message="Wrong answer");
    }

    set MagicNumber(Number num) {
        return num*2;
    }
}

Usage:

myInst = new MyClass();

writeoutput(myInst.TheAnswerToLifeTheUniverseAndEverything); // 42

myInst.MagicNumber = 2;
writeoutput(myInst.MagicNumber); // 4

I could see merit in either direction. I’d probably lean more toward the .NET implementation personally - it groups the setter/getter better, and less verbose.

I suppose a case could be made for neither, in favor of an “onMissingMember” callback instead, but that’s a different thought.

2 Likes

I really like the idea, and my preference is for the C# version. This is mostly borne from the fact those were my first experience with implicit accessors (/mutators).

Either would be fine, and a good addition to the language IMO.

Nice one.


Adam

I had started writing a proposal for this very thing a while back, shortly before lucee was announced. When it was I held off and didnt finish, so this is what I had and is a bit unpolished.

One of the best parts of AS3 was the ability to define accessor and mutator methods for properties. From the outside, it looks as though you are just using properties, but it actually allows you to define a method for either. It also allows you to define just an accessor and not a mutator for read only properties (set everything in a constructor and now you have immutable instances).

Example proposed syntax:

public void function set property(required any property) {
	_.property = property;
}

public any function get property() {
	return _.property;
}

These could also be generated automatically similar to the way that accessors=true works today.

property type propertyName get set;

Of course these could always be overridden by an actual method which would also allow us to do other things, such as validation.

From the outside, the consumer would use these methods just like properties on an instance and it would get rid of boilerplate get and set prefixes and unnecessary parentheticals.

o.property = "value"; //would throw an error if no mutator is defined
var x = o.property;

This will greatly simplify the external API of simple data object classes and provide an easy way to create read-only properties. If the language was extended to allow us to use these anywhere a struct can be used today (their interface would be the same), even better. Think the param struct passed to queryExecute()

We could do most of this with a library if we had a new special method in cfc’s like onMissingProperty(missingPropertyName, accessorOrMutator, missingValue) but in this case I would prefer this ability to be built into the language for performance reasons. I would still love to have an onMissingProperty method though. Perhaps onMissingProperty would allow us to try it out with some libraryies first to find the right syntax.

2 Likes