Page 1 of 1

Namespace aliasing/masking?

PostPosted: Thu Apr 29, 2010 1:45 pm
by CodexArcanum
I just started playing with Cobra (really cool language so far) and decided to try implementing something fun to get a feel for the language. I'm referencing into a C# library (which itself is a wrapper for some C code, zany!) and this library has it's own version of type Console. This is ambiguous with System.Console.

In the sample code for the library, this is resolved by
Code: Select all
using Console = Wrapper.Console;
to mask the System version. You can also employ the using ... = ... syntax to alias a particularly arduous namespace.

Here comes the question: Is there any equivalent syntax in Cobra, or am I forced to use the fully qualified name?

Re: Namespace aliasing/masking?

PostPosted: Thu Apr 29, 2010 8:59 pm
by Charles
We don't have namespace aliasing at this time. I'm not opposed to it, but it hasn't been implemented yet.

Re: Namespace aliasing/masking?

PostPosted: Fri Apr 30, 2010 12:55 am
by CodexArcanum
Well, that's fair enough. Thanks for the swift answer!

It would be a useful feature, but it's not essential. Actually, it turned out that I only needed to reference the namespace once anyway since type inference took care of most other usages in the file. So I guess the language features in Cobra mostly amortize the extra verbosity in the occasional colliding namespace call.

Re: Namespace aliasing/masking?

PostPosted: Fri Apr 30, 2010 11:39 am
by CodexArcanum
Ok, I have to back to this issue because I am getting killed by it in my current test program. Here's some code real quick:

Code: Select all
@ref "libtcodWrapper"

use libtcodWrapper

namespace RogueTest

    class Game implements IDisposable

        var scon as libtcodWrapper.Console?
        var con as RootConsole?


Now, in another post elsewhere, Chuck pointed out that my "var scon" was being picked up dynamically, which was not my intention although it was working. I know what type that is and want to save myself the penalties of dynamic variables by just specifying it, as I did above.

But that won't build. Cobra is telling me that it "Cannot find type for "libtcodWrapper.Console?". I get the same message if I take off the "?" but I know I'll need it because RootConsole needed it as they are nilable. However, if I take the namespace off, I get: "Ambiguous reference "Console" found in namespaces "libtcodWrapper" and "System"."

Which you'd kind of expect. So I'm not sure what to do here. If I don't use dynamic types the code won't build, but I'd rather use the static type since I do know it.


Hmmm... looking at the library code, Console is a public class but it only has one internal constructor. Would that cause problems in Cobra with it not being able to locate it?

Re: Namespace aliasing/masking?

PostPosted: Fri Apr 30, 2010 10:33 pm
by Charles
This was a bug in Cobra plain and simple. Now fixed. I didn't have time to create an isolated test case, but I can get past this problem in my local "tcod" directory where I duplicated the issue.

You will need to install from source for now. See the HowToInstallFromSource wiki page. It's fairly easy.

Re: Namespace aliasing/masking?

PostPosted: Sat May 01, 2010 6:44 pm
by CodexArcanum
Yeah, I had installed from source before (the batch file is very convenient!) so I just had to get an SVN version instead and reinstall.

I just tried my program again (with proper namespacing) and it seems to be working! Good fix! Also removing the (?) gave the expected nillable error (and an additional warning about how "if .scon" will always be true) so that's much more helpful. Thanks very much for correcting this so quick.

Also I hadn't realized you'd be getting the library yourself to test it out, but should have guessed. In the future I'll provide a link so you won't have to go digging it up yourself as I presume you must have done.

Re: Namespace aliasing/masking?

PostPosted: Sun May 02, 2010 2:41 am
by Charles
Glad to hear things are going better for you.

Re: Namespace aliasing/masking?

PostPosted: Sun May 02, 2010 4:17 am
by hopscc
I opened ticket:201 for this enhancement.

Theres a description of the C# construct at MSDN - Using Directive.

I'd suggest the cobra syntax should be augmented from
Code: Select all
use <namespace> [from <library-name>]


to something like
Code: Select all
use <namespace> [as <alias-name>] [ from <library-name>]


so that the above example can be done something like
use Wrapper.Console as Console

and a convenience like
use Cobra.Lang.CobraCore as CC 
...
print CC.version
args = CC.commandLineArgs

becomes possible.