Advertisement

Constness in property accessors

Started by April 21, 2021 12:56 PM
3 comments, last by WitchLord 3 years, 7 months ago

I used to register only the following on an object: (from C++)

CSomething@ get_Something() property;
void set_Something(CSomething@ value) property;

And I would omit the setter function if it's read-only.

This works very well, but I need to have a separate const method now too. For example, the following declarations:

CSomething@ get_Something() property;
const CSomething@ get_Something() const property;
void set_Something(CSomething@ value) property;

This however gives an error when I try using the const getter:

The property 'Something' has mismatching types for the get and set accessors
const CSomething@ CFoo::get_Something() const
void CFoo::set_Something(CSomething@)

Is it still possible to do something like this?

(Ps: I am using asEP_PROPERTY_ACCESSOR_MODE = 2, so I don't actually pass the “property” decorator, if that makes any difference.)

I'll need to think about this. Technically it is possible but it conflicts with the design for virtual properties, either the property is CSomething@ or it is const CSomething@.

How would this work for with the alternate syntax?

CSomething @Something { get const { return _Something; } set { @_Something = value; } }

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

Would it make sense to automatically return “const CSomething@” for getters on const objects, and not allow the setter? Or would that break stuff?

What is read only when the object is const is the handle itself, not the object that handle refers to.

But, maybe with an extra keyword to indicate the behaviour you want it would be possible.

Perhaps some other language has already done something similar. I'll need to do some research.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement