implicit data type conversion functions from Class A to class B

Started by
2 comments, last by randomguy123451 11 years, 7 months ago
----------
http://alexzuzin.com...imp-my-library/
refer to the above url, in scala
Implicit type conversion is a simple idea with powerful repercussions. Scala lets you define methods that convert an instance of one type into an instance of another – say, a java.lang.String into your.own.SuperString. When such a method is in scope (properly imported), you can use a java.lang.String anywhere you need to use your.own.SuperString, and the compiler will automatically call the converter method. Here’s an example adapted from the venerable library Scalaz:
class BooleanW(isTrue: Boolean)
object BooleanW{
implicit def BooleanTo(b: Boolean): BooleanW = new BooleanW(b)
implicit def BooleanFrom(b: BooleanW): Boolean = b.isTrue
}
If, like above, you define conversions both to and from the new class, you essentially get the ability to extend a class or trait and use the extension in all existing APIs – just like Ruby’s open classes, but with static type checking. This pattern is so useful that it has its own name – “Pimp My Library.” It’s pervasive in Scala – you’ll see and use it a lot. The specific additions to existing types are usually described as “pimps.”
-------------
Now, in angelscript:
I will like to ask that if I have a class ZFraction (value type) and I want that if somebody wants to make lots of objects and its mathematics, then whether I can define somekind of "Implicit" data conversion function (without explicit casting or constructor pattern), so that string can be converted to ZFraction by that function. This will really ease the scripting to real scripting level.

ZFraction frac1 = "22/3"; // i want to use like this

ZFraction frac1 = ZFraction("22/3"); // i do not want to use(pseudocode): ZFraction frac1 =ZFraction("22/7") spewn all over my source code
ZFraction frac1 = cast <ZFraction>("22/3") // i do not want this pseudocode spewn all over my source code. Though this is fine for "explicit" data type conversion, but it is not good for "implicit" data type conversion
Advertisement
asBEHAVE_IMPLICIT_VALUE_CAST

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

To expand a bit on this. The asBEHAVE_IMPLICIT_VALUE_CAST is registered with the "from" type, to allow it to implicitly cast to another type.

The "to" type can also register a value cast from another type, by implementing the appropriate constructor, though it is currently only usable in explicit casts.

In case of assignment operator, the ZFraction type can implement an opAssign method that takes a string. This is the most efficient form, as it avoids the step of first converting the string.

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

Thanks a lot. :-)

This topic is closed to new replies.

Advertisement