Inline imports redivivus

Zach Tollen zach at mystic.yeah
Sat Mar 12 23:57:09 UTC 2022


On Saturday, 12 March 2022 at 19:33:16 UTC, Daniel N wrote:
> Out of your new alternatives, I like this one the best.
> ```d
> x = :std.datetime:SysTime;
> ```
>
> 1) It is symmetric same as ||
> 2) : is already used in import x:y
> 3) : is not used in expressions unlike | which is quite common.
> 4) We can keep $ for some future codegen features.

Thanks. Those are good points. I think I even agree with you.

Regarding (3), there is an exception in the case of the 
conditional operator (`? :`), but I don't think it's a huge 
problem. The worst visual confusion here might be something like:
```d
{
    auto x = :mymod.question:ask ? :another.modname:asdf
          ? :moremods:answer1 : :finalmod.three:okay
          : :mymod.answer:two
}
```
To do a general comparison of the versions, let's take this code 
snippet from 
[phobos](https://github.com/dlang/phobos/blob/16cb085b584f100fa677e2e64ff6b6dbb4921ad1/std/algorithm/comparison.d#L88):

The way it is now:
```d
if (Values.length != 0)
{
     foreach (uint i, ref v; values)
     {
         import std.functional : binaryFun;
         if (binaryFun!pred(value, v)) return i + 1;
     }
     return 0;
}
```
Ugh. Not my favorite.

Now let's look at the [pull 
request](https://github.com/dlang/druntime/pull/1756/files) which 
was already merged, which allows you to write the following:
```d
if (Values.length != 0)
{
     foreach (uint i, ref v; values)
     {
         if (imported!"std.functional".binaryFun!pred(value, v)) 
return i + 1;
     }
     return 0;
}
```
I'd say this represents 70% of the total aesthetic improvement 
between the versions. Just being able to inline imports makes 
most of the difference here — not bad for a technique with no 
language support! (We're not talking about compilation speed, 
although direct language support would allow the compiler to 
bypass the normal lookup process and go straight to module where 
the import is located, so it would speed up.)

Using my language-supported long form `import` syntax, the result 
is:
```d
if (Values.length != 0)
{
     foreach (uint i, ref v; values)
     {
         if (import.std.functional:binaryFun!pred(value, v)) 
return i + 1;
     }
     return 0;
}
```
I'll give this 5% of the total aesthetic improvement, from the 
previous version to this.

Finally, with `:modname:symbol`:
```d
if (Values.length != 0)
{
     foreach (uint i, ref v; values)
     {
         if (:std.functional:binaryFun!pred(value, v)) return i + 
1;
     }
     return 0;
}
```
I give this the remaining 25% of the total improvement.

I'm curious what other people think.


More information about the Digitalmars-d mailing list