Embrace the from template?

Jonathan Marler johnnymarler at gmail.com
Fri Aug 24 13:54:51 UTC 2018


On Friday, 24 August 2018 at 10:58:29 UTC, aliak wrote:
> On Friday, 24 August 2018 at 06:41:35 UTC, Jonathan Marler 
> wrote:
>> Ever since I read 
>> https://dlang.org/blog/2017/02/13/a-new-import-idiom/ I've 
>> very much enjoyed using the new `from` template.  It unlocks 
>> new idioms in D and have been so useful that I thought it 
>> might be a good addition to the core language.  I've found 
>> that having it in a different place in each project and always 
>> having to remember to import it makes it much less ubiquitous 
>> for me.
>>
>> One idea is we could add this template to `object.d`.  This 
>> would allow it to be used from any module that uses druntime 
>> without having to import it first.  The template itself is 
>> also very friendly to "bloat" because it only has a single 
>> input parameter which is just a string, extremely easy to 
>> memoize.  Also, unless it is instantiated, adding it to 
>> object.d will have virtually no overhead (just a few AST nodes 
>> which would dwarfed by what's already in object.d).  It would 
>> also be very easy to add, a single PR with 4 lines of code to 
>> druntime and we're done.
>>
>> Of course, if we don't want to encourage use of the `from` 
>> template then this is not what we'd want.  Does anyone have 
>> any data/experience with from?  All I know is my own usage so 
>> feel free to chime in with yours.
>
> One of the first things I do after a dub init is create a file 
> called internal.d with the from template in it. My only gripe 
> about this template is it's "autocompletion-able-ness" in IDEs 
> and if that can be handled.
>
> I would not want it globally imported though, "from" is quite 
> popular as an identifier and D doesn't let you use keywords as 
> identifiers.
>
> Cheers,
> - Ali

Good to know others are using it.  Of course making it a core 
part of the language would mean that IDEs would be free to add 
support for it, whether it was added to `object.d` or with some 
other means such as a new syntax, i.e.

(import std.stdio).writefln(...)

I didn't quite understand your last point.  Adding `from` to 
`object.d` wouldn't make it a keyword, it would still be an 
identifier.  And you could still use it as an identifier in your 
own code.



More information about the Digitalmars-d mailing list