Sign in to follow this  
chewbakka

handle property compile error

Recommended Posts

If I just modify the tutorial sample as code to this, the compilation fails.
[source lang="cpp"]
interface IFace
{
void Foo();
}

class Thing : IFace
{
void Foo()
{
}
}

class Hub
{
IFace@ IFace
{
get
{
return iface;
}
set
{
@iface = value;
}
}

private IFace@ iface;
}

float calc(float a, float b)
{
// Print the value that we received
Print("Received: " + a + ", " + b + "\n");

// Print the current system time
Print("System has been running for " + GetSystemTime()/1000.0 + " seconds\n");

Hub@ hub = Hub();
hub.IFace = Thing();

// Do the calculation and return the value to the application
return a * b;
}
[/source]
Basically it fails if I create a property with set when the property type is a class handle. The get works well.

Share this post


Link to post
Share on other sites
What is the compilation error that you get? Is it the following?

[code]
test (29, 15) : Error : There is no copy operator for the type 'IFace' available.
[/code]

That error is correct. The expression [font=courier new,courier,monospace]hub.IFace = Thing();[/font] attempts to do a value assignment of the IFace property, which it cannot. The correct expression is [font=courier new,courier,monospace]@hub.IFace = Thing();[/font] that do a handle assignment to update the reference in IFace handle.

Regards,
Andreas Edited by Andreas Jonsson

Share this post


Link to post
Share on other sites
It was that yes. While it makes sense, the confusion comes from the documentation. I taught that the property accessors are equivalent with the set_ and get_ functions, whose are willing to work like set_IFace( Thing() ) of course.
An off topic question.
Is there a reason that the language don't accept array properties in this form too:
[source lang="cpp"]string firstString;
string secondString;

string stringArray[]
{
get
{
switch( index )
{
case 0: return firstString;
case 1: return secondString;
}
return "";
}
set
{
switch( index )
{
case 0: firstString = value; break;
case 1: secondString = value; break;
}
}
}[/source]
With the index parameter coming form the property declaration like the value.

Thank you.

Share this post


Link to post
Share on other sites
[quote name='brumi' timestamp='1344325004' post='4966928']
An off topic question.
Is there a reason that the language don't accept array properties in this form too:
[source lang="cpp"]string firstString;
string secondString;

string stringArray[]
{
get
{
switch( index )
{
case 0: return firstString;
case 1: return secondString;
}
return "";
}
set
{
switch( index )
{
case 0: firstString = value; break;
case 1: secondString = value; break;
}
}
}[/source]
With the index parameter coming form the property declaration like the value.

Thank you.
[/quote]

You can do that with the operator methods:
[source lang="cpp"]
class StringArray
{
string firstString;
string secondString;

string get_opIndex(int idx)
{
switch( index )
{
case 0:
return firstString;
case 1:
return secondString;
}
return "";
}

void set_opIndex(int idx, string &in value)
{
switch( index )
{
case 0:
firstString = value;
break;
case 1:
secondString;
break;
}
}
}
[/source]

It's not exactly what you're looking for, though -- for one thing, it's not possible to use with arbitrary properties, so you'd have to specialize a class each time you wanted this behavior.

Edit: Source tag is so bad. Edited by Jake Albano

Share this post


Link to post
Share on other sites
I know I know, but being able to use this form, would make a more consistent look on the code, showing what is a property on the first sight. you can use operator methods with non arrays as well, but the code is much readable writing it in property form.

Share this post


Link to post
Share on other sites
[quote name='brumi' timestamp='1344325004' post='4966928']
It was that yes. While it makes sense, the confusion comes from the documentation. I taught that the property accessors are equivalent with the set_ and get_ functions, whose are willing to work like set_IFace( Thing() ) of course.
[/quote]

The property accessors are equivalent with the set_ get_ functions. In fact the int prop { get; set; } form is internally translated to and compiled as get_prop and set_prop methods. You can even call the get_prop, set_prop directly if you wish.

However, the difference is what the assignment means:

IFace = Thing(); is translated to get_IFace() = Thing(); because the syntax means to do a value assignment on the object referred to by the handle.

@IFace = Thing(); is translated to set_IFace(Thing()); because the syntax means to do a reference assignment to reassign which object is referred to by the handle.



[quote]
An off topic question.
Is there a reason that the language don't accept array properties in this form too:
With the index parameter coming form the property declaration like the value.

Thank you.
[/quote]

It just hadn't been suggested to me before. I'll look into implementing something like this for a future release. :)

The shorter syntax for the property accessors was implemented by InvalidPointer and added in version 2.22.1. Edited by Andreas Jonsson

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this