• Create Account

## Finding matching Operators

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.

5 replies to this topic

### #1Jason Goepel  Members

Posted 26 September 2013 - 02:46 PM

I have two classes registered with an implicit value cast:

engine->RegisterObjectBehaviour("TypeA", asBEHAVE_IMPLICIT_VALUE_CAST, "double f() const", ...);
engine->RegisterObjectBehaviour("TypeB", asBEHAVE_IMPLICIT_VALUE_CAST, "double f() const", ...);


The idea behind these classes is that the script writer may use them, in most cases, interchangeably with doubles.  Math operators, in particular, need to work.

TypeA x;
TypeB y;
double z;

z = x * y; 

However, asCCompiler::CompileOperator fails to find a matching operator.  I encountered the following comment in "as_compiler.cpp", line 10814:

// If both operands are objects, then we shouldn't continue
if( lctx->type.dataType.IsObject() && rctx->type.dataType.IsObject() )
{
asCString str;
Error(str, node);
ctx->type.SetDummy();
return -1;
} 

Is there a reason we "shouldn't" continue?  It seems like if both the left and right types objects one could examine all implicit value casts and attempt to find an operator match for those.  Obviously, if one didn't limit the examination to primitive types that search could be infinite.  I was going to attempt such a search.  Do you have any thoughts or reservations?

In this particular case I could derive TypeA and TypeB from a "DoubleValue" class which would register all math operations in which I would be interested for the derived types.

I would like to clarify.  I am not insisting on this feature, I am just "thinking out loud".  I haven't given this a tremendous amount of thought, and my first impression is that there would be many complications that could arise from such a change. C++ does identify operators from casts though, so I suppose there shouldn't be any fundamental issues.

Edited by Jason Goepel, 26 September 2013 - 03:42 PM.

### #2Jason Goepel  Members

Posted 26 September 2013 - 03:19 PM

I took a stab at it and found this change to give me what I want.  I search for implicit value casts to the same primitive type.  I don't think would work an the official implementation, but in general it captures what I am seeking.  What do you think?

### #3Andreas Jonsson  Moderators

Posted 26 September 2013 - 03:47 PM

I think it would be a bit too drastic. What if someone wants to have the implicit cast but don't want the operators to work? It wouldn't be possible to disable the operators if the compiler automatically searches for a possible cast to primitives.

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

### #4Jason Goepel  Members

Posted 27 September 2013 - 06:47 AM

You may be correct about it being too drastic, but I don't think it's quite as bad as you think.  C++ already allows this sort of thing.  This simple example compiles:

class ClassA
{
short x;
public:
ClassA(short a) : x(a) {}

operator double() const { return (double)x; }
};

class ClassB
{
int x;

public:
ClassB(int b) : x(b) {}

operator double() const { return (double)x; }
};

ClassA x(1);
ClassB y(2);

double z = x + y;


AngelScript already doesn't really allow one do disable operators if one has registered an implicit value cast.  Even though TypeA does not provide an opMul method, nor does it provide an implicit cast to an integer, the following will still compile and run:

TypeA x;

int y = x * 5;



It seems like once somebody has registered an implicit value cast, he can no longer expect to restrict any use where that type could be converted to value-cast type.  Registering an implicit value cast currently allows all the binary operators to work as long as the other type is a primitive type.

### #5Andreas Jonsson  Moderators

Posted 27 September 2013 - 10:33 AM

I'll take this into consideration.

I've put this on my to-do list for a time when I can sit down for a while to think about the implications better.

Thanks,

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

### #6FDsagizi  Members

Posted 27 September 2013 - 09:18 PM

r = en->RegisterObjectBehaviour("Vec3", asBEHAVE_IMPLICIT_VALUE_CAST, "string f()", asMETHOD(Vec3,ToString), asCALL_THISCALL ); nu_assert( r >= 0 );

this code work fine:

Vec3 my_vec;
string str = my_vec; ( Vec3 can convert to string )
But, this code wrong:

Vec3 vec;
string s1;
string s2 = s1 + vec3; // for this case be best if AS automaticly find variant when he can append to string

Edited by FDsagizi, 27 September 2013 - 09:19 PM.

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.