Re:How I Came to Write D (2014)
by z3t4 on Sunday June 03, 2018 @01:28PM
(#17220594)
D is one of the few languages that allows named scoped imports.
[ Parent
| on HN ]
It's tricky to do an apples-to-apples comparison. Imports in D conflate two distinct concepts (in a helpful way): declaring a module dependency, and introducing names into the local namespace. Not all languages work the same, though. Ocaml, for example, has great support for namespace updates (`let module M = TheModule in ...` and `let open TheModule in ...` and `module MyModule = struct open TheModule ...`). But they are slightly less significant in Ocaml, because any module that has been linked into the program is available everywhere in the program: you can just write `let result = TheModule.call_something () in ...`, anywhere you like, with no "imports". Linkages (dependencies) are handled externally at the tooling level.
It's certainly convenient that an `import` inside a D function body can tackle both issues at once.
What are those?
I don't know D, and I had a quick Google, but couldn't find anything by exactly that name - apologies for the lazy question.
Re:How I Came to Write D (2014)
by skocznymroczny on Monday June 04, 2018 @05:43AM
(#17226115)
void foo()
{
import std.stdio;
writeln("sup")
}
Re:How I Came to Write D (2014)
by vanderZwan on Sunday June 03, 2018 @02:26PM
(#17220913)
I think that was a thing that Alex highlighted as an unexpectedly great thing in the language in one of his D talks (on mobile, can't find it right now) because it enabled all kinds of neat organisational tricks
You can do that in Ocaml too, let open Foo in expr.
Are you referring to https://dlang.org/spec/module.html#scoped_imports ?
If so, I believe imports in Python and Rust work similarly, and, say, JS's 'require' seems to too.
Python can also import only some names from a module:
from module import name, name, name
and can also run a module import under control of a try/except block, so you can trap the ImportError and do something about it. A typical use is to try to import the C version (for performance) of a module, and if that fails, fall back to the pure Python version of the same module; e.g. cElementTree and ElementTree: try:
import cElementTree
except ImportError:
import ElementTreeSorry, mistake in the last snippet above, it should really be:
try:
import cElementTree as ElementTree
except ImportError:
import ElementTree
because otherwise, you have to know which import succeeded, because that changes the name by which you reference the module and the items in it.