Hello,
Is it possible (or planned) to declare template functions, classes or methods in AngelScript?
I know it is possible to use a template system when registering the application interface,
however my script classes will not always be bound to C++ code.
Just like C++/C#/Java, I want to factorise code like this:
void sayHello<T>(obj)
{
obj.hello(); // Compiler error if T doesn't have a hello method
}
class Entity
{
Component@[] _components;
Cmp_T@ addComponent<Cmp_T>()
{
// Compiler error if T doesn't derives from Component
Cmp_T@ cmp = Cmp_T();
_components.push(cmp);
return cmp;
}
Cmp_T@ getComponent<Cmp_T>()
{
//...
}
}
class Spawner<T>
{
T spawn()
{
return T();
}
}
I'm asking this because I find that templates are a good feature in strong typed languages, for many reasons:
- Type-safe statements
- Less code repetitions
- Reduces overuse of explicit casts
- Makes code reusable more quickly
- Allows defining new containers directly in AS
Just like C++, the compiler could generate the corresponding implementations at compile-time,
or check the types at run-time (but it sounds less efficient to me).
I'm not dreaming on something as hard as C++ templates, C# template system is fine already, and hasn't the drawbacks of debugging C++ ones.
I know that interfaces exist to handle some of these situations,
but personnally I find these a bit old-fashioned and too heavy to be used in scripting, where quick iteration should be an advantage.
Is this feature possible to implement/is planned?