Advertisement

"using namespace" support?

Started by November 12, 2024 04:15 PM
9 comments, last by WitchLord 5 days, 3 hours ago

Hello! In my projects, I frequently use AngelScript, and one limitation I’ve noticed is the absence of a "using namespace <name>" feature like in C++. This can make working with namespaces cumbersome at times.

To address this, I decided to implement my own support for it. It mostly works in a similar way to C++ (though function access has some differences). Here’s an example to illustrate:

void g_func() {}

namespace App
{
    class Class{};
    void func() { print("APP::FOO"); }
    enum Enum {};

    void g_func() {}
}

namespace Foo
{
    void foo_example() {};
}

namespace Bar
{
    using namespace App;

    class Class{};
    void func() { print("BAR::FOO"); }
    enum Enum {};

    void example1()
    {
        func(); // Error, multiple declaration (Bar::func and App::func)
        g_func(); // Error, multiple declaration (g_func and App::g_func)

        Enum a; // Ok, using Bar::Enum;
        App::Enum b; // Ok, using App::Enum

    } 

    void example2()
    {
        {
            using namespace Foo;
            foo_example(); // Ok, called Foo::foo_example();
        }
        foo_example(); // Error, foo_example is not visible here;
    }
}

// If you change App to Bar in the next line, an error will occur because Bar will automatically inherit App namespace visibility.
using namespace App; 
class A : Class {}; // Ok, parent is App::Class

The difference with the functions I mentioned earlier is that I haven’t yet figured out how to prioritize the namespace from which the function is called.

If this implementation is acceptable for public use, I will be happy to send a patch with the changes

This looks good to me.

I'd be happy to incorporate this into the next release. Please send me a patch and I'll review it and merge it into the library.

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

Advertisement

I sent you the patch by email

Hmm, I didn't get it. I checked my spam folder as well.

Did you send it to andreas@angelcode.com?

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

Yes, I sent to this mail. I will try to send again

Already sent again

Advertisement

I think your e-mail is getting blocked somewhere. I'm getting the notifications from gamedev.net that you commented here, but your e-mail is not reaching me.

Are you sending it as an compressed file in an attachment? Try renaming the extension to something else, e.g. zi_. Or you can use some cloud service (gdrive, onesource, etc) to share the file.

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

Here is a link to GDrive

Thanks. I've downloaded the patch. I'll review it as soon as possible.

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

I've finally incorporated this patch into the library.

https://sourceforge.net/p/angelscript/code/2995/

As I ran it through my regression test suite I identified some changes that were needed on top of the patch, so you may find it working slightly differently that what you had originally submitted. For example, there where issues with property accessors not hiding real properties of the same name, and access to enum values without specifying the scope. The search through the visible namespaces should also not continue up the parent namespace when a symbol has already been found, so that was also fixed.

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

Advertisement