Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Implicit cast from a primitive type


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.

  • You cannot reply to this topic
4 replies to this topic

#1 _Vicious_   Members   -  Reputation: 240

Like
0Likes
Like

Posted 10 February 2012 - 11:49 AM

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.

Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3262

Like
0Likes
Like

Posted 10 February 2012 - 08:44 PM

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

#3 _Vicious_   Members   -  Reputation: 240

Like
0Likes
Like

Posted 11 February 2012 - 03:39 AM

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);


#4 Andreas Jonsson   Moderators   -  Reputation: 3262

Like
0Likes
Like

Posted 11 February 2012 - 02:46 PM

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

#5 _Vicious_   Members   -  Reputation: 240

Like
0Likes
Like

Posted 12 February 2012 - 02:48 AM

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




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.



PARTNERS