Sign in to follow this  

Template methods in scripts?

This topic is 1108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Edited by Zylann

Share this post


Link to post
Share on other sites

This topic is 1108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this