Jump to page: 1 24  
Page
Thread overview
imports in functions
Jun 12, 2011
Walter Bright
Jun 12, 2011
Adam D. Ruppe
Jun 12, 2011
Andrej Mitrovic
Jun 12, 2011
Jonathan M Davis
Jun 12, 2011
Andrej Mitrovic
Jun 12, 2011
Dmitry Olshansky
Jun 13, 2011
Robert Clipsham
Jun 13, 2011
Andrej Mitrovic
Jun 13, 2011
Andrej Mitrovic
Jun 13, 2011
KennyTM~
Jun 14, 2011
Andrej Mitrovic
Jun 14, 2011
Brad Roberts
Jun 14, 2011
Andrej Mitrovic
Jun 14, 2011
Andrej Mitrovic
Jun 14, 2011
Andrej Mitrovic
Jun 14, 2011
Andrej Mitrovic
Jun 14, 2011
Brad Roberts
Jun 14, 2011
Andrej Mitrovic
Jun 17, 2011
Jacob Carlborg
Jun 18, 2011
Andrej Mitrovic
Jun 20, 2011
Ary Manzana
Jun 20, 2011
Andrej Mitrovic
Jun 20, 2011
Nick Sabalausky
Jun 20, 2011
Andrej Mitrovic
Jun 21, 2011
Andrej Mitrovic
Jun 20, 2011
Jacob Carlborg
Jun 20, 2011
Ary Manzana
June 12, 2011
Nobody seems to have noticed yet, but yesterday I removed the restriction preventing import declarations from being used in functions. These now work:

void test1()
{
    import std.c.stdio;
    printf("hello world\n");
}

void test2()
{
    static import std.c.stdio;
    std.c.stdio.printf("hello world\n");
}

https://github.com/D-Programming-Language/dmd/commit/d5fbd53aa8d8452dce2514944575e654d387477a

I believe this can lead to better encapsulation of code, especially when using mixins, versioning and other conditional compilation constructs.
June 12, 2011
On 06/12/2011 04:15 PM, Walter Bright wrote:
> Nobody seems to have noticed yet, but yesterday I removed the
> restriction preventing import declarations from being used in functions.
> These now work:
>
> void test1()
> {
> import std.c.stdio;
> printf("hello world\n");
> }
>
> void test2()
> {
> static import std.c.stdio;
> std.c.stdio.printf("hello world\n");
> }
>
> https://github.com/D-Programming-Language/dmd/commit/d5fbd53aa8d8452dce2514944575e654d387477a
>
>
> I believe this can lead to better encapsulation of code, especially when
> using mixins, versioning and other conditional compilation constructs.

Fabulous. std.benchmark already uses the feature, and from a mixin no less:

https://github.com/andralex/phobos/commit/c5f2778a0fc393d6bd17ebec4765b28937575c31

One interesting aspect of the feature is that if import is used from within a template, it's not actually imported unless the template is instantiated.

Andrei
June 12, 2011
This is awesome!

Another benefit here is code running programs are simplified. For "D script" kind of things, a technique I use is to simply wrap some code inside a main function. Almost every feature worked there - nested functions, structs, classes, etc. Now imports do too! Yay!
June 12, 2011
On 06/12/2011 04:33 PM, Adam D. Ruppe wrote:
> This is awesome!
>
> Another benefit here is code running programs are simplified. For
> "D script" kind of things, a technique I use is to simply wrap some
> code inside a main function. Almost every feature worked there -
> nested functions, structs, classes, etc. Now imports do too! Yay!

This bodes well for your "run this online" thing, too. All we need to do is wrap a main() around the example code and it should work.

Andrei
June 12, 2011
This seems like a way to simulate namespaces in C++, right? I wouldn't know, but it looks similar to that 'using foo' trick.
June 12, 2011
On Sun, 12 Jun 2011 14:15:27 -0700, Walter Bright wrote:

> Nobody seems to have noticed yet, but yesterday I removed the restriction preventing import declarations from being used in functions. These now work:
> 
> void test1()
> {
>      import std.c.stdio;
>      printf("hello world\n");
> }
> 
> void test2()
> {
>      static import std.c.stdio;
>      std.c.stdio.printf("hello world\n");
> }
> 
> https://github.com/D-Programming-Language/dmd/commit/
d5fbd53aa8d8452dce2514944575e654d387477a
> 
> I believe this can lead to better encapsulation of code, especially when using mixins, versioning and other conditional compilation constructs.

Cool!  This is a definite improvement. :)

-Lars
June 12, 2011
On 13.06.2011 1:15, Walter Bright wrote:
> Nobody seems to have noticed yet, but yesterday I removed the restriction preventing import declarations from being used in functions. These now work:
>
> void test1()
> {
>     import std.c.stdio;
>     printf("hello world\n");
> }
>
> void test2()
> {
>     static import std.c.stdio;
>     std.c.stdio.printf("hello world\n");
> }
>
> https://github.com/D-Programming-Language/dmd/commit/d5fbd53aa8d8452dce2514944575e654d387477a 
>
>
> I believe this can lead to better encapsulation of code, especially when using mixins, versioning and other conditional compilation constructs.

Also very helpful for unittests, thanks.

-- 
Dmitry Olshansky

June 12, 2011
On 2011-06-12 15:08, Andrej Mitrovic wrote:
> This seems like a way to simulate namespaces in C++, right? I wouldn't know, but it looks similar to that 'using foo' trick.

C++ namespaces are completely different. Everything in a namespace has to be referenced by its namespace explicitly unless you use using. So, without using, you need to do stuff like

std::vector<int> v;

whereas with

using namespace std;
vector<int> v;

you don't need the std:: tag anymore. Using namespace affects _everything_ after it, which is why it's pretty much verboten in header files (otherwise it would pollute the global namespace). And whether you have a using statement or not, as soon as you #include a file, everything in it is visible in the current file. Namespaces just segregate the names so that they don't clash.

D modules are very different. As soon as you import a module, everything in that module is visible (though you can't use anything in it unless it's public or the current module is in the same package and it's package or if you're deriving from class in that module and the symbol in question is protected). You don't have to use the module name when using _any_ of its symbols unless names clash or you imported the module statically. It's kind of like you automatically have a using statement all of the time, except that it's much more sophisticated and handles name clashes much better.

By importing within a function, you're saying that only that function has access to the symbols within the module being imported. As for as the rest of the module is concerned, the imported module does not actually exist. You're restricting what can see it.

So, I'm not quite sure what you mean by this simulating C++ namespaces. They both deal with how symbols are brought into and viewable in the current file, but they're very different.

- Jonathan M Davis
June 12, 2011
On 6/13/11, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
< snip>

So much for my blurry recollection of C++ features. :-)
June 13, 2011
On 6/12/11 5:08 PM, Andrej Mitrovic wrote:
> This seems like a way to simulate namespaces in C++, right? I wouldn't
> know, but it looks similar to that 'using foo' trick.

I hope not :o). C++ namespaces are quite lacking.

Andrei
« First   ‹ Prev
1 2 3 4