| Thread overview | |||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 12, 2011 imports in functions | ||||
|---|---|---|---|---|
| ||||
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 Re: imports in functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 Re: imports in functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 Re: imports in functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 Re: imports in functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | 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 Re: imports in functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 Re: imports in functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 Re: imports in functions | ||||
|---|---|---|---|---|
| ||||
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 Re: imports in functions | ||||
|---|---|---|---|---|
| ||||
On 6/13/11, Jonathan M Davis <jmdavisProg@gmx.com> wrote: < snip> So much for my blurry recollection of C++ features. :-) | ||||
June 13, 2011 Re: imports in functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply