Inline imports redivivus

Zach Tollen zach at mystic.yeah
Fri Mar 11 06:48:16 UTC 2022


On Monday, 7 March 2022 at 10:27:08 UTC, Per Nordlöw wrote:
> https://forum.dlang.org/post/sdehom$10fi$1@digitalmars.com
>
> On Friday, 23 July 2021 at 13:55:34 UTC, Andrei Alexandrescu 
> wrote:
>
>> If it works well in practice, a future language proposal could 
>> take
>> `_import!"std.datetime".SysTime` to the simpler and better
>> `import(std.datetime).SysTime`.
>
> Yes, please!

The reason I wanted to revive this redivivus — so to say — is 
because I believe the following syntax is near-optimal for the 
task at hand:

`import.std.datetime:SysTime`

 From my perspective there is no need to rely on advanced template 
features since inline imports are so basic that they should be 
adopted as a language feature instead.

The above syntax is excellent because:

- It is grammatically unambiguous
- It requires no parentheses or other enclosing marks
- It resembles selective imports

That is exactly what it is: a use-once, selective import. You use 
it as a drop-in replacement for the identifier you want to import.

In reviewing the historical proposals even as far back as DIP1005 
[1], it seems that the syntax above simply wasn't stumbled upon. 
Therefore I wanted it to be evaluated. "Destroy!"

To give a little more food for thought, in addition to the above 
syntax, I wanted to introduce a stunning syntax sugar, which uses 
`$` instead of `import .` as a special token to activate the 
sequence:

`$std.datetime:SysTime`

This is the tersest fully qualified inline import possible. I 
suppose it is strange on the eyes. But don't implement it unless 
you want people to use it, because people can hardly resist a 
concise syntax like this!

Anyway, those are my proposals. Here are some previous proposals 
and why I have problems with them:

```d
imported!"std.datetime".SysTime // - already implemented in 
object.d [2]
                                 // - best you can do with 
existing language
                                 // - can't use keyword `import`
                                 // - requires `""` and `!`

import(std.datetime).SysTime    // parens unnecessary
(import std.datetime).SysTime   // ditto
std.datetime::SysTime           // grammatically ambiguous 
(module name unclear)

::std.datetime:SysTime          // if we put the `::` first it 
becomes viable
                                 // it's a different version of 
the `$`

```

[1] 
https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1005.md#syntactic-alternatives

[2] https://github.com/dlang/druntime/pull/1756/files



More information about the Digitalmars-d mailing list