Templated function in script

Started by
4 comments, last by iraxef 9 years, 9 months ago

I have a lot of code which looks like this:


MyDerivedType@ foo = cast<MyDerivedType>(database.FindObject("foo"));

...

MyOtherDerivedType@ bar = cast< MyOtherDerivedType >(database.FindObject("bar"));

Is there any existing way to have something like a templated function? The syntax I'd like to have is (for example):


MyDerivedType@ foo = database.FindObject<MyDerivedType>("foo");

Thank you.

Advertisement

In that case will be useful to pass somehow classes as argument.

bool FindObject(class c)

{

for( int i = 0; i < objects.length(); i++ )

if( cast< c >( objects[ i ] ) !is null )

return true;

return false;

}

Or do more flexibly system, like this:

type - new class for storing information about type.

class MyClass

class MyClass : MyDerivedClass

MyClass obj;

type myType1 = typeof( MyClass );

type myType2 = typeof( obj );

type myDerivedType = typeof( MyDerivedClass );

Comparsion with operators ==, !=, <, >, <=, >= and is (equal to <=):

myType1 X myType2; true: ==, <=, >=, is; false: !=, <, >;

myType1 X derivedObj; true: !=, <=, <, is; false: ==, >, >=;

derivedObj X myType1; true: !=, >=, >; false: ==, <, <=, is;

This is can be implemented for now, if will be added possibility to pass class as parameter.

I can pass class name as string and resolve it in runtime (typeof("MyClass")), but this is not good practice, need checking in compile time.

I think maybe i can do it for now with templates, something like this:

type t = typeof<MyClass>().get();

where typeof is template class with get function returning instance of type class.

It's on my to-do list to implement support for template functions/methods. Though it will probably take a while before I get to it.

Until the template functions/methods are supported perhaps you might consider using the variable argument type. With it you would be able to have a syntax like this:

MyDerivedType@ foo;
database.FindObject("foo", @foo);

...

MyOtherDerivedType@ bar;
database.FindObject("bar", @bar);

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

That's a nice step in the right direction! Thank you.

Now you just need a variable return type :)

The variable argument type is working well. A good step in the right direction!

This topic is closed to new replies.

Advertisement