handle property compile error

Started by
6 comments, last by chewbakka 11 years, 8 months ago
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.
Advertisement
Thanks. I'll look into this

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

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

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

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.

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

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.

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

Thank you for the clarification.

This topic is closed to new replies.

Advertisement