Implicit cast from a primitive type

Started by
3 comments, last by _Vicious_ 12 years, 2 months ago
Hello Andreas,

is it possible to register an implicit or a regular cast behaviours for primitive types? I'd like to be able to cast primitive types to strings without calling some specialized functions or adding overloads for that.

Correction: the name of this thread should say from a primitive type, not to.
Advertisement
I've updated the topic title.

Implicit casts from primitives without function overloads are not yet supported. However, explicit casts are easily implemented by registering the appropriate constructor, e.g.:


engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f(int)", asFUNCTIONPR(string_construct, (int, string*), void), asCALL_CDECL_OBJLAST);


The script can then do the following:


int a;
string s;
s = string(a);


Implicit casts from primitives will eventually be implemented by allowing the compiler to implicitly find and call the appropriate constructors. I'll probably need to have a new behaviour for this though, i.e. asBEHAVE_IMPLICIT_CONSTRUCT, so the application can choose which constructors can be called implicitly or not.

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

Well, yes, I'm aware of the explicit casts using constructors/factories, however what I had in my was something different:

void SetProperty(cString name, cString value)
{
..
}
...
int position = 100;
self.setProperty('position', position);

without having to do something like this:

self.setProperty('position', cString(position));

or even this:

self.setProperty('position', "" + position);
Until I have the implicit calls to the constructors implemented, what you want can only be implemented through function overloads, i.e:


void SetProperty(cString name, int value) { ... }

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

yeah, figured :) that's a rather unpractical approach though, but thanks anyway :)

This topic is closed to new replies.

Advertisement