4 ms·
D is one of the few languages that allows named scoped imports.
by z3t4 8y ago
D is one of the few languages that allows named scoped imports.
- wtetzner 8y agoScala is the only other one I'm aware of.
- vanderZwan 8y agoI 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
- twic 8y agoWhat 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.
- skocznymroczny 8y agovoid foo() { import std.stdio; writeln("sup") }
- xfer 8y agoYou can do that in Ocaml too, let open Foo in expr.
- gmfawcett 8y agoIt'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.
- dbaupp 8y agoAre you referring to https://dlang.org/spec/module.html#scoped_imports 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.
- vram22 8y agoPython 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 ElementTree
- vram22 8y agoSorry, 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.