Advertisement

Property setter doesn't return

Started by January 17, 2017 02:56 PM
2 comments, last by WitchLord 7 years, 10 months ago
The following code:
	class Foo
	{
		private int _bar;
		int bar
		{
			get { return _bar; }
			set { _bar = value; }
		}
	}

	void Main()
	{
		Foo x, y;

		// You can't do this
		x.bar = y.bar = 10;

		// You have to do this instead:
		x.bar = 10;
		y.bar = 10;
	}
Complains that "No matching signatures to 'Foo::set_bar(void)'". Instead, you have to set them individually.

If I do this:
int get_bar() { return _bar; }
int set_var(int v) { _bar = v; return _bar; }
It says "The property has no set accessor".

Not sure if this is a bug or a missing feature though.

It would be a missing feature.

I'll look into what it would take to allow chaining assignments involving virtual properties. It would probably involve a different change than to have the set-accessor return a value though. Probably the compiler should be rewriting the statement to the following:

// this 
x.bar = y.bar = 10;
 
// becomes
auto temp = 10;
y.set_bar(temp);
x.set_bar(temp);

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

Regarding the above solution, please bear in mind the following code:


class Object {
  float real;
  private float _virt;
  float get_virt() const {
    return _virt;
  }
  void set_virt(float val) {
    _virt = val;
  }
}
void main() {
  Object obj;
  any a;

  // Sets a to float 123.f
  a = obj.real = 123;

  // Would intuitively set a to float 123.f but in the proposed solution sets it to int 123 instead
  a = obj.virt = 123;
}

Good point. I'll keep it in mind

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