Re: Idioms for the D programming language

2021-02-11 Thread Imperatorn via Digitalmars-d-announce
On Thursday, 11 February 2021 at 20:12:36 UTC, Walter Bright 
wrote:

Now #5 on the front page of https://news.ycombinator.com/news

The page being discussed:

https://p0nce.github.io/d-idioms/

and on reddit:

https://www.reddit.com/r/programming/comments/lhssjp/idioms_for_the_d_programming_language/


đź‘Ť


Re: Idioms for the D programming language

2021-02-11 Thread James Lu via Digitalmars-d-announce
On Thursday, 11 February 2021 at 20:12:36 UTC, Walter Bright 
wrote:

Now #5 on the front page of https://news.ycombinator.com/news


I've gotten Dlang to the frontpage of HN 3 other times:


Specification for the D Programming Language 
https://news.ycombinator.com/item?id=21993208

D 2.0.93.0 https://news.ycombinator.com/item?id=23859448
Ask HN: Why do you use Rust, when D is available? 
https://news.ycombinator.com/item?id=23494490


If you have any particularly interesting and specific pages on D, 
feel free to send them to me, so I can post them to HN at the 
optimal time. (Furthermore, if you authored it, you should a 
comment that explains why you made it and offering to answer 
questions– I have found this can turn a regular post into a 
frontpage post.)


Re: Idioms for the D programming language

2021-02-11 Thread H. S. Teoh via Digitalmars-d-announce
On Thu, Feb 11, 2021 at 12:12:36PM -0800, Walter Bright via 
Digitalmars-d-announce wrote:
[...]
> https://p0nce.github.io/d-idioms/

Not bad, but it seems to be missing some of the newer idioms.
Like the templated static this trick for transparently encoding
compile-time information at runtime.  For example:

import std.regex;
void main(string[] args) {
foreach (arg; args) {
if (arg.match(Re!`some.*regex(pattern)+`)) {
... // do something
}
}
}

template Re(string reStr) {
Regex!char re;
static struct Impl {
static this() {
re = regex(reStr);
}
}
string Re() { return re; }
}

The Re template captures the regex string at compile-time and injects it
into Impl's static this(), which compiles the regex at program startup
at runtime, then the eponymous function Re() simply returns the
precompiled value.

The result:
- Regexes are automatically picked up at compile-time;
- But expensive compile-time generation of the regex (which consumes
  lots of compiler memory and slows down compilation) is skipped;
- Compilation of the regex happens only once upon program startup and
  cached, and thereafter is a simple global variable lookup.
- Multiple occurrences of the Re template with the same regex is
  automatically merged (because it's the same instantiation of the
  template).

D features used:
- compile-time string parameters
- static this()
- eponymous templates
- reuse of template instantiations that have the same arguments


[...]
> https://www.reddit.com/r/programming/comments/lhssjp/idioms_for_the_d_programming_language/

There doesn't appear to be any discussion happening here.


T

-- 
If it breaks, you get to keep both pieces. -- Software disclaimer notice


Idioms for the D programming language

2021-02-11 Thread Walter Bright via Digitalmars-d-announce

Now #5 on the front page of https://news.ycombinator.com/news

The page being discussed:

https://p0nce.github.io/d-idioms/

and on reddit:

https://www.reddit.com/r/programming/comments/lhssjp/idioms_for_the_d_programming_language/