Jump to content
  • Advertisement
Sign in to follow this  
Zylann

Template methods in scripts?

This topic is 1323 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
Advertisement

It would be possible to implement, but is not currently planned.

 

I just feel it is a bit overkill for a scripting language.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!