Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


- - - - -

C#-style property getters/setters


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 InvalidPointer   Members   -  Reputation: 1439

Like
0Likes
Like

Posted 20 October 2011 - 04:27 PM

Just gauging interest in the concept, though I don't know what percentage of AS users frequent these forums. I've got a (second-generation, actually) implementation going that makes minimal changes to the actual back-end but makes a few enhancements to the parser/builder interfaces that let the compiler infer or generate function signatures on-the-fly. The net result is that you can now use the super-sexy C# property syntax like so:

In interfaces:
int TestProperty
{
	get;
	set;
}

AppInterface@ AltTestProperty
{
	get;
}

float AnotherTestProperty
{
	set;
}

In actual classes and/or global property definitions:
int SomeRandomInteger;

int AGlobalProperty
{
	set
	{
		// You even get the implicit 'value' parameter gratis!
		SomeRandomInteger = value;
	}
	get
	{
		return SomeRandomInteger;
	}
}

while still 'talking' to them via the bog-standard
AGlobalProperty = 50;
in external code. If anyone's interested, give me a holler here, but I'll probably just ship this off to WitchLord as soon as I work out a few parser kinks (for the interested, defining multiple getters/setters within a single property block doesn't fall-through quite as elegantly as I expected, the major result being some odd error messages) but at time of writing this does all compile and work more or less correctly. Next on the agenda is maybe using this to do some cool foreach trick :)

EDIT: I'll probably also make a few syntax tweaks to allow property getters to be optionally const, but I'm still thinking about how I want that to work.
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.

Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3413

Like
0Likes
Like

Posted 22 October 2011 - 12:41 PM

I like it, and would gladly incorporate it into the script library.

I especially like the fact that you only modified the parser and builder, which I interprets as the fact that you're translating the above syntax to the more explicit syntax that I currently use in AngelScript. This is good because it maintains backwards compatibility and also allows the script writer to fallback on the more explicit syntax when they want to write something that is goes against these automated code generations (for example a get accessor that returns a handle, but a set accessor that receives a reference.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#3 randomguy12345   Members   -  Reputation: 105

Like
0Likes
Like

Posted 23 October 2011 - 06:25 AM

thumbs up to "InvalidPointer" and "WitchLord" for incorporating this into angelscript. This brings angelscript a step closer to powerful languages.

my theme for embedded language is to have all the object oriented, functional and other-developer friendly "features of the core language itself", BUT without the baggage of a large runtime library.
properties are very important part of that. Posted Image



#4 InvalidPointer   Members   -  Reputation: 1439

Like
0Likes
Like

Posted 23 October 2011 - 11:18 AM

I like it, and would gladly incorporate it into the script library.

I especially like the fact that you only modified the parser and builder, which I interprets as the fact that you're translating the above syntax to the more explicit syntax that I currently use in AngelScript. This is good because it maintains backwards compatibility and also allows the script writer to fallback on the more explicit syntax when they want to write something that is goes against these automated code generations (for example a get accessor that returns a handle, but a set accessor that receives a reference.

Exactly. The older, 'first-generation' approach created a separate object aspect category, imaginatively titled 'virtual properties' :) which sort of worked as an almagation of 'real' properties and actual methods. You could attach getter/setter functions arbitrarily and had a slightly more natural approach to app-side reflection at the cost of pretty extensive internal rewrites and a lot more code duplication than I think was healthy. This does seem like a good approach in the long run (and could actually be made using the infrastructure I did keep; it was actually lifted more or less wholesale) but ultimately I wasn't comfortable making large refactorings without a *very* thorough understanding of why the existing API is structured the way it is. One more thing to add to the roadmap? :)

I will also mention I made a few changes to how the naming scheme in particular works internally (with some justification, bear with me) along with this-- while both work entirely independently and you're free to swap back to the old style in the official version, it seems to me that prefixing accessors with 'get_' and 'set_' in the new style could be *extremely* confusing when script writers are attempting to resolve syntax errors-- the signature will obviously look a lot like that of a 'real' function which may be confusing when the actual code will look really different. While arguments could be made that things like line and column numbers can be used as a failsafe, I think this actually gives IDEs and occasionally script writers too much credit! :P As a result, getters are now named "<propertyname> {getter}" and setters named "<propertyname> {setter}" respectively. This style has a number of advantages-- no coding style interactions, the declarations themselves actually look a lot more like the code they refer to, and lastly 'break' the parser in such a way so as to render them uncallable via traditional script syntax-- you *must* use the property syntax as opposed to freely mixing
set_AGlobalProperty(50)
and
AGlobalProperty = 50
from the example in the OP. The main downside to this is that using app-side reflection to look up functions by declaration/register application-defined system functions becomes slightly more complex-- I'm adding some special-case methods for this since it seems the user probably has all the required contextual information anyway. Simply iterating over all functions requires no changes and works out-of-the-box.

And, as the final touch, do we like
int ConstTestProperty
{
	get const
	{
		return 5;
	}
}
or do we prefer
int AlternateConstTestProperty
{
	const get
	{
		return 6;
	}
}
I personally much prefer #1 as it feels a lot more like the 'void Foo::Bar() const' you'd see in normal const member functions and is probably easier to implement, but whatever floats everyone's boat.

EDIT: Also, does it make sense for setters to be declared const? I mean, I can think of cases where you might just be modifying a global property (this could be a good way to do static properties, btw) without changing the state of the actual object itself, but I'm weighing if this is considered poor enough form for it to be killed at a syntactic rather than stylistic level.
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.

#5 ekimr   Members   -  Reputation: 107

Like
0Likes
Like

Posted 25 October 2011 - 06:43 AM

Seems to me as if the const keyword should be valid in both circumstances.



int Property
{
	// const int get_Property() const;
	const get const
	{
		return m_iProperty;
	}
	// int set_Property();
	set
	{
		m_iProperty = value;
	}
 }

Also, shouldn't the setter return the value being set so that calls can be chained?
Rantings, ravings, and occasional insight from your typical code-monkey: http://angryprogrammingprimate.blogspot.com/

#6 InvalidPointer   Members   -  Reputation: 1439

Like
0Likes
Like

Posted 25 October 2011 - 10:16 AM

Seems to me as if the const keyword should be valid in both circumstances.



int Property
{
	// const int get_Property() const;
	const get const
	{
		return m_iProperty;
	}
	// int set_Property();
	set
	{
		m_iProperty = value;
	}
 }

Also, shouldn't the setter return the value being set so that calls can be chained?

Nope, that would mean circumventing the actual getter outright. As it stands, chaining works for the examples above, though it's more of a two-step process. The setter is first invoked, and then the getter is called which will mutate the new value however it sees fit-- that's what will come out 'on the other side' of the chain. In this regard, I follow the original getter/setter conventions exactly. In fact, I don't change that part of the code at all! :)

Re: return type mixing-- I'm not sure that's a good idea to swap types around like that. You can decorate the actual emulated type however you see fit-- things like
const Object@ AConstProperty
{
	get const
	{
		return constPropertyBacking;
	}
	set
	{
		constPropertyBacking = value;
	}
}
will work just fine. I will take that to mean the const 'postfix' style is what we prefer, though, so that's how I'll implement it.

Also, made a few changes to the built-in array type so that it now uses virtual properties. You can use the more mundane
for( uint32 i = 0; i < someArray.length; ++i )
to retrieve the array length, but it gets really interesting when you change this value-- the array will actually now resize itself to the specified length!

EDIT: const behavior implemented, just developing a method for specifying this for app-defined classes. Other than that, I think we're in business and patches shall be forthcoming.
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.

#7 Andreas Jonsson   Moderators   -  Reputation: 3413

Like
0Likes
Like

Posted 25 October 2011 - 08:32 PM

I look forward to the patches. I hope to be able to release version 2.22.0 by the end of next week, so I'll probably not include this until then, but perhaps for version 2.22.1 (unless it requires changes to the API, because then it would have to wait until 2.23.0).
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#8 InvalidPointer   Members   -  Reputation: 1439

Like
0Likes
Like

Posted 26 October 2011 - 08:59 AM

I look forward to the patches. I hope to be able to release version 2.22.0 by the end of next week, so I'll probably not include this until then, but perhaps for version 2.22.1 (unless it requires changes to the API, because then it would have to wait until 2.23.0).


In that case, I'll actually hold off for a bit and bundle this with the final/override keyword feature set, since this would require some changes for consistency anyway. I can probably put together some documenation as well.

EDIT:
Changes are tested, and a patch file for revision 1015 created and sent.
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.

#9 Andreas Jonsson   Moderators   -  Reputation: 3413

Like
0Likes
Like

Posted 16 November 2011 - 08:04 PM

I've checked in the contribution now in revision 1043.

I've a few changes but it works mostly the same way that you implemented it. Notable changes:

- The keywords final, override, get, and set are not reserved, as I do not want to prevent anyone from using them in variable and function names
- I decided not to go with the naming convention 'property {getter}' and 'property {setter}'. So the current way of implementing property accessors still work. With this I also do not have to provide all the new interface methods that you implemented (RegisterVirtualPropertyAccessor, etc).

Your main argument to provide the different naming convention can likely be fixed by providing more informative error messages instead. The manual will also explicitly state what is done behind the scenes when declaring a virtual property using the new syntax.

I'll probably continue to enhance this with automatic implementation for the getter and setter, as well as automatic declaration of the real property, if it is used in the get/set implementation.

An improvement to the final/override keywords is the implementation of the keyword abstract as well.


Thanks a lot for the contribution.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS