handle property compile error

This topic is 2175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 on other sites
Thanks. I'll look into this

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

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

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 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 on other sites

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.

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 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 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.

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.

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 on other sites
Thank you for the clarification.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631780
• Total Posts
3002314
×