Sign in to follow this  
Jason Goepel

Finding matching Operators

Recommended Posts

Jason Goepel    797

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;
    str.Format(TXT_NO_MATCHING_OP_FOUND_FOR_TYPES_s_AND_s, lctx->type.dataType.Format().AddressOf(), rctx->type.dataType.Format().AddressOf());
    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

Share this post


Link to post
Share on other sites
Jason Goepel    797

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?

Share this post


Link to post
Share on other sites
WitchLord    4677

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.

Share this post


Link to post
Share on other sites
Jason Goepel    797

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.

Share this post


Link to post
Share on other sites
FDsagizi    514
i thinking about this when i use Vec3 and convert to string class.
 
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

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