Forums

Namespace aliasing/masking?

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Namespace aliasing/masking?

Postby CodexArcanum » Thu Apr 29, 2010 1:45 pm

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?
CodexArcanum
 
Posts: 21

Re: Namespace aliasing/masking?

Postby Charles » Thu Apr 29, 2010 8:59 pm

We don't have namespace aliasing at this time. I'm not opposed to it, but it hasn't been implemented yet.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Namespace aliasing/masking?

Postby CodexArcanum » Fri Apr 30, 2010 12:55 am

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.
CodexArcanum
 
Posts: 21

Re: Namespace aliasing/masking?

Postby CodexArcanum » Fri Apr 30, 2010 11:39 am

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?
CodexArcanum
 
Posts: 21

Re: Namespace aliasing/masking?

Postby Charles » Fri Apr 30, 2010 10:33 pm

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Namespace aliasing/masking?

Postby CodexArcanum » Sat May 01, 2010 6:44 pm

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.
CodexArcanum
 
Posts: 21

Re: Namespace aliasing/masking?

Postby Charles » Sun May 02, 2010 2:41 am

Glad to hear things are going better for you.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Namespace aliasing/masking?

Postby hopscc » Sun May 02, 2010 4:17 am

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.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 29 guests