www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What is your favorite D feature?

reply Seb <seb wilzba.ch> writes:
Hi,

I am currently trying to modernize the D code example roulette on 
the dlang.org front page [1]. Hence, I would love to hear about 
your favorite feature(s) in D.
Ideas:
- favorite language construct
- favorite code sample
- "only possible in D"

Before you ask, yes - I want to add a couple of cool examples to 
dlang.org (and yep the roulette rotation is currently broken [2]).

[1] 
https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
[2] https://github.com/dlang/dlang.org/pull/1757
Jun 21 2017
next sibling parent reply Mike <none none.com> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:

 I would love to hear about your favorite feature(s) in D.
Beginning with most favorite: - CTFE - static if - If you don't consider that part of CTFE - Template Mixins - Templates - Pretty much goes along with the top 2 - String Mixins - Unit Tests DIP1000 may make that list too, if I ever get around to trying it out. Mike
Jun 21 2017
parent reply Seb <seb wilzba.ch> writes:
On Thursday, 22 June 2017 at 01:00:42 UTC, Mike wrote:
 Beginning with most favorite:
  - CTFE
  - static if - If you don't consider that part of CTFE
  - Template Mixins
  - Templates - Pretty much goes along with the top 2
  - String Mixins
  - Unit Tests

 DIP1000 may make that list too, if I ever get around to trying 
 it out.
Nice list, but I was actually looking for more concise, actionable snippets or idea(s) for such. I should have made this clearer. Sorry. For some I have already submitted PRs - the other are still missing a good "WoW" snippet. Ideas?
  - CTFE
-> https://github.com/dlang/dlang.org/pull/1758
  - static if - If you don't consider that part of CTFE
Do you know a more concise example than my commonPrefix one from the Tour? https://tour.dlang.org/tour/en/gems/traits
  - Template Mixins
TBD
  - Templates - Pretty much goes along with the top 2
TBD
  - String Mixins
-> https://github.com/dlang/dlang.org/pull/1762
  - Unit Tests
TBD
Jun 22 2017
parent Mike <none none.com> writes:
On Thursday, 22 June 2017 at 07:10:14 UTC, Seb wrote:

 Nice list, but I was actually looking for more concise, 
 actionable snippets or idea(s) for such.
There are some real gems in this DConf Talk: https://www.youtube.com/watch?v=yMNMV9JlkcQ I started enumerating them on the Wiki here (https://wiki.dlang.org/Tutorials#Design_Patterns) but alas it's still incomplete. Mike
Jun 22 2017
prev sibling next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
My fav is that familiar code just works.
Jun 21 2017
prev sibling next sibling parent reply "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Thu, Jun 22, 2017 at 12:48:25AM +0000, Seb via Digitalmars-d wrote:
 Hi,
 
 I am currently trying to modernize the D code example roulette on the
 dlang.org front page [1]. Hence, I would love to hear about your
 favorite feature(s) in D.

 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"
Slices! And preferably in an example where it beats C performance by not needing to duplicate strings everywhere. Built-in unittests... ddoc'd unittests! Though it's hard to think of an example showing this off that's short enough to work for the roulette. Sane template syntax. Template alias parameters. Manipulation of template argument lists. UFCS. Compile-time introspection + UDAs. Loop over a struct defining a set of program configuration parameters, and generate code for parsing command-line arguments that fills in the struct based on field definitions. (You could just transform the struct members into getopt arguments, as implementing this from scratch could be a bear... and ugly to look at. :-D) std.process making it dead easy to invoke an external program, capture its output, all without the ugliness of manually dealing with fork(), execv(), and waitpid().
 Before you ask, yes - I want to add a couple of cool examples to
 dlang.org
[...] You could search for "your code here" in the forum -- that used to be the instructions on submitting code examples back before the website was revamped, and IIRC there have been a handful of suggestions, though AFAIK none was ever actually added to the roulette. T -- Those who don't understand Unix are condemned to reinvent it, poorly.
Jun 21 2017
next sibling parent Jon Degenhardt <jond noreply.com> writes:
On Thursday, 22 June 2017 at 01:13:43 UTC, H. S. Teoh wrote:
 On Thu, Jun 22, 2017 at 12:48:25AM +0000, Seb via Digitalmars-d 
 wrote:

 [snip]

 Slices!  And preferably in an example where it beats C 
 performance by not needing to duplicate strings everywhere.
 ...
For slices the example in blog post I wrote might serve (https://dlang.org/blog/2017/05/24/faster-command-line-tools-in-d/). To shorten it drop the associative array component. For example, sum the values in a specific field. --Jon
Jun 21 2017
prev sibling parent Seb <seb wilzba.ch> writes:
On Thursday, 22 June 2017 at 01:13:43 UTC, H. S. Teoh wrote:
 Slices!  And preferably in an example where it beats C 
 performance by not needing to duplicate strings everywhere.
There's one in the queue, feel free to vote for or destroy it: https://github.com/dlang/dlang.org/pull/1756
 Built-in unittests... ddoc'd unittests!  Though it's hard to 
 think of an example showing this off that's short enough to 
 work for the roulette.
Yes ... ideas welcome ;-)
 Sane template syntax. Template alias parameters. Manipulation 
 of template argument lists.
Do you have anything specific in mind?
 UFCS.
Absolutely agreed, but how do we show this? Range - one of the best uses cases - are already shown.
 Compile-time introspection + UDAs.  Loop over a struct defining 
 a set of program configuration parameters, and generate code 
 for parsing command-line arguments that fills in the struct 
 based on field definitions.  (You could just transform the 
 struct members into getopt arguments, as implementing this from 
 scratch could be a bear... and ugly to look at. :-D)
Ok - I gave it a shot, but it got quite long. Any ideas on trimming it down? https://github.com/dlang/dlang.org/pull/1762
 std.process making it dead easy to invoke an external program, 
 capture
 its output, all without the ugliness of manually dealing with 
 fork(),
 execv(), and waitpid().
I realized std.parallelism gives an excellent showcase as well: https://github.com/dlang/dlang.org/pull/1760 And created another one for std.process: https://github.com/dlang/dlang.org/pull/1761 Feedback welcome!
 You could search for "your code here" in the forum -- that used 
 to be the instructions on submitting code examples back before 
 the website was revamped, and IIRC there have been a handful of 
 suggestions, though AFAIK none was ever actually added to the 
 roulette.
Thanks, but I can only remember spam being posted with "your code here". The search also doesn't show any results for me...
Jun 21 2017
prev sibling next sibling parent reply Brad Anderson <eco gnuk.net> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently broken 
 [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
A very simple vibe app could be added using dub's single-file package format. Something like (I haven't tried this): /+ dub.sdl: name "hello" dependency "vibe-d" version="~>0.8.0-rc.1" +/ import vibe.d; shared static this() { auto settings = new HTTPServerSettings; settings.port = 8080; listenHTTP(settings, &handleRequest); } void handleRequest(HTTPServerRequest req, HTTPServerResponse res) { if (req.path == "/") res.writeBody("Hello, World!", "text/plain"); } // Dependencies fetched, compiled, cached, built against, and result executed // in one command: // $ ./hello.d
Jun 21 2017
parent Seb <seb wilzba.ch> writes:
On Thursday, 22 June 2017 at 01:42:10 UTC, Brad Anderson wrote:
 A very simple vibe app could be added using dub's single-file 
 package format.
Hmm this is a great idea, but it wouldn't be "runnable" on the web. Here's a PR for discussion: https://github.com/dlang/dlang.org/pull/1763
 Something like (I haven't tried this):
You can even go more minimal - the following works: /+ dub.sdl: name "hello_vibed" dependency "vibe-d" version="~>0.8.0-rc.1" versions "VibeDefaultMain" +/ import vibe.d; shared static this() safe { auto settings = new HTTPServerSettings; settings.port = 8080; listenHTTP(settings, (req, res) { res.writeBody("Hello, World: " ~ req.path); }); }
Jun 21 2017
prev sibling next sibling parent Eugene Wissner <belka caraus.de> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently broken 
 [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
ranges nice operator overloading
Jun 21 2017
prev sibling next sibling parent Jon Degenhardt <jond noreply.com> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
A couple more: - std.conv.to - Safe, convenient conversions that just work. - std.regex - Really well done. May be hard to illustrate all the capabilities in a few examples - Pragmatic functional programming constructs. No guessing about performance. Purity with local mutable variables. Interaction with UCFS. --Jon
Jun 21 2017
prev sibling next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
	"friendship ".writeln = ((_) => "is"~_)(" magic");


	"friendship ".writeln = ((_) => "is"~_)(" magic");

just couldn't resist the temptaion, sorry.
Jun 21 2017
prev sibling next sibling parent reply bauss <jj_1337 live.dk> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently broken 
 [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
These: https://p0nce.github.io/d-idioms/
Jun 21 2017
parent Seb <seb wilzba.ch> writes:
On Thursday, 22 June 2017 at 06:02:54 UTC, bauss wrote:
 On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently 
 broken [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
These: https://p0nce.github.io/d-idioms/
Hehe, I know about p0nce's great work, but I was asking for _your_ favorite D code snippets ;-) Is there any idiom that you like in particular or think it's a good "show-off"?
Jun 22 2017
prev sibling next sibling parent reply Paulo Pinto <pjmlp progtools.org> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently broken 
 [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
Being a GC enabled systems programming language, following the footsteps of Mesa/Cedar and Modula-3.
Jun 21 2017
parent Seb <seb wilzba.ch> writes:
On Thursday, 22 June 2017 at 06:08:07 UTC, Paulo Pinto wrote:
 On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently 
 broken [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
Being a GC enabled systems programming language, following the footsteps of Mesa/Cedar and Modula-3.
Yes :) ... but how do we turn this into an example snippet with < 10 LoC? (I should have been a bit clear on what I was looking for, sorry).
Jun 22 2017
prev sibling next sibling parent reply Ecstatic Coder <ecstatic.coder gmail.com> writes:
Lack of verbosity.

Clear concise code, thanks to the automatic initialization of 
class members, native strings/arrays/maps/slices, UFCS, 
declaration-order independence, etc.

class TOTO
{
     bool IsCool;
     int Age;
     TUTU[] Tutus;
     TOTO[string] Totos;

     void Foo( TUTU tutu )
     {
         Tutus ~= tutu;
         Totos[ tutu.Name ] = tutu.Toto;
     }
}

class TUTU
{
     string Name;
     TOTO Toto;

     this( string name )
     {
         Name = name;
         Toto = new TOTO;
     }
}

void Bar(
     ref TOTO toto
     )
{
     toto.IsCool = !toto.IsCool;
}

void main()
{
     TUTU tutu;
     TOTO toto;

     tutu = new TUTU( "tutu" );

     toto = new TOTO;
     toto.Foo( tutu );
     toto.Bar();
}

Absolutely no syntactic noise !!!

This is often overlooked, while D easily beats all its direct 


Just try to implement the same code as simply in C++ and you will 
be convinced that this is D's strongest feature...

And this is also what makes D feel like a super-powered 
JavaScript when I use it :)
Jun 21 2017
parent reply Seb <seb wilzba.ch> writes:
On Thursday, 22 June 2017 at 06:27:11 UTC, Ecstatic Coder wrote:
 Absolutely no syntactic noise !!!

 This is often overlooked, while D easily beats all its direct 


 Just try to implement the same code as simply in C++ and you 
 will be convinced that this is D's strongest feature...

 And this is also what makes D feel like a super-powered 
 JavaScript when I use it :)
Thanks a lot, but I was looking for small snippets (< 10 LoC) than could be used to show the awesomeness of D at the roulette on https://dlang.org Any chance you could make your point a bit conciser? :)
Jun 22 2017
next sibling parent Ecstatic Coder <ecstatic.coder gmail.com> writes:
 Thanks a lot, but I was looking for small snippets (< 10 LoC) 
 than could be used to show the awesomeness of D at the roulette 
 on https://dlang.org
 Any chance you could make your point a bit conciser? :)
I agree with you, indeed it is much too long for a carousel ;) And it should be made even simpler. But I still think that the first and only sample code someone should read when coming for the first time on the D website should be this kind of example, which shows how simple and straightforward the D code looks like. It's very important to reassure the visitor curious about D on how easy it is to learn and use D, showing him that D's syntax is familiar, and the code is very readable and concise. Some first-time visitors may be meta-programming experts, but many others (including me) are just looking for a simpler alternative to existing languages, which allows them to quickly program their stuff in an easy and efficient way. And for those looking for a language which allows to program complicated things in a simpler way, as D indeed allows to do it, this should also be shown. But I don't think that this should be the first example someone sees. That said, I'm not totally against code carousel. The one on the Python website is very well done. https://www.python.org/ Its code snippets are very simple on purpose. Python's landing webpage is very encouraging, and shows that this scripting language can be picked up quickly.
Jun 22 2017
prev sibling parent Patrick Schluter <Patrick.Schluter bbox.fr> writes:
On Thursday, 22 June 2017 at 07:00:17 UTC, Seb wrote:
 Thanks a lot, but I was looking for small snippets (< 10 LoC) 
 than could be used to show the awesomeness of D at the roulette
enum E {IN = -1, V1, V2, V3, X1 } mixin({ string code = "enum EBIT : ulong { "~ "init = 0,"; /* We set the dummy init value to 0 */ foreach(Code; __traits(allMembers, E)) { static if(Code == "IN") code ~= "INVALID = -1,"; else code ~= Code~"= 1 << E."~Code~","; } code ~= " ALL = -1, return code ~ "}"; }()); Generates an enum which values depend on the values of another enum. In C or other languages I would needed to write the 2nd enum myself enum EBIT { INVALID = -1, V1= 1 << E.V1, V2= 1 << E.V2, V3= 1 << E.V3, X1= 1 << E.X1, } For a 4 value enum it doesn't look such a big deal but in my real project coming from C, the first enum holds more than 50 regular values and 3 special values. When I have to add or remove a value I have to edit the first enum, but also the 2nd enum, 5 derived lookup tables and 2 functions using a big switch/case with the enum values. In D I edit only the first enum, the rest is auto-generated. As 2nd trick/nice thing here is a function geenrating a switch/case with the enums. string E2thing(E val) { switch(val) { foreach(e; __traits(allMembers, E)) static if(e != "IN") mixin(`case E.`~e~`: return "`~e~`";`); default: return "UNDEFINED"; } }
Jun 22 2017
prev sibling next sibling parent reply qznc <qznc web.de> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently broken 
 [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
H. S. Teoh calendar: https://wiki.dlang.org/Component_programming_with_ranges /** * Formats a year. * Parameters: * year = Year to display calendar for. * monthsPerRow = How many months to fit into a row in the output. * Returns: A range of strings representing the formatted year. */ auto formatYear(int year, int monthsPerRow) { enum colSpacing = 1; return datesInYear(year) // Start by generating all dates for the given year .byMonth() // Group them by month .chunks(monthsPerRow) // Group the months into horizontal rows // Format each row .map!(r => r.formatMonths() // By formatting each month .array() // Storing each month's formatting in a row buffer // Horizontally pasting each respective month's lines together .pasteBlocks(colSpacing) .join("\n")) // Insert a blank line between each row .join("\n\n"); }
Jun 22 2017
parent Seb <seb wilzba.ch> writes:
On Thursday, 22 June 2017 at 08:52:27 UTC, qznc wrote:
 On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 [...]
H. S. Teoh calendar: https://wiki.dlang.org/Component_programming_with_ranges [...]
Yes that's a great example, but sadly not standalone - we can only use Phobos for the example snippets on dlang.org ... The examples need to compile and run, so that a reader can modify and play with them.
Jun 22 2017
prev sibling next sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 I would love to hear about your favorite feature(s) in D.
Arrays include length along with the pointer. Very easy built-in array support with foreach Ranges, that don't treat their input types as 'pointers' and get all levels of confusion, including that you'd have to include two 'maybe pointers'. Constraints on templates, making limiting and specializing very easy. static if & version removing the pre-processor...
Jun 22 2017
prev sibling next sibling parent k-five <vhsu30 yahoo.com> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently broken 
 [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote: Universal Function Call Syntax ( UFCS ) Sample code on my gitgub + screen-shot, https://github.com/k-five/dren main code: recursively renaming files based on Regexes dirEntries( ".", ( args[ 4 ] == "-r" ? SpanMode.depth : SpanMode.shallow ), false ) .filter!( file => !file.name.matchFirst( regex( args[ 1 ] ) ).empty() ) .filter!( file => ( args[ 3 ] == "-f" || args[ 3 ] == "-d" ? ( args[ 3 ] == "-f" ? !file.isDir : !file.isFile ) : ( !file.isSymlink ) ) ) .map!( file => file.name ) .each!( ( string result ) => ( args[ 5 ] == "-y" ? rename( result, replaceFirst( result, regex( args[ 1 ] ), args[ 2 ] ) ) : writeln( "print: ",result, " >> ", replaceFirst( result, regex( args[ 1 ] ), "\033[1;32m" ~ args[ 2 ] ~ "\033[m" ) ) ) );
Jun 22 2017
prev sibling next sibling parent Guillaume Piolat <first.last gmail.com> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
I think "alias this" is a good contender. But actually I like how D deal with names. alias and alias template parameters exposes the identifier, being a type or a value. It is very clean.
Jun 22 2017
prev sibling next sibling parent reply Andrey <andrey kabylin.ru> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently broken 
 [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
My favorite features: - CFTE - UDA - delegates - UFCS - property - Template mixins - Contracts and unittests
Jun 22 2017
parent Andrey <andrey kabylin.ru> writes:
UDA snippet:
 class MyView : View {
      ViewWidget Button okButton;
      ViewWidget("cancelButton") Button myButton;
      GroupViewWidgets Button[3] buttons;
 
      OnClickListener("okButton")
     void onOkButtonClick(Widget widget) {
         ...
     }
 
      Shortcut("TestGroup.cancel")
     void someShortcutAction() {
         ...
     }
 
      OnClickListener("closeButton")
      OnClickListener("cancelButton")
     void onCancelButtonClick(Widget widget) {
         ...
     }
 }
Jun 22 2017
prev sibling next sibling parent Kagamin <spam here.lot> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently broken 
 [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
Is it to mirror python examples? Shouldn't there be simple examples for newbie programmers too?
Jun 23 2017
prev sibling next sibling parent reply Anonymouse <asdf asdf.net> writes:
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently broken 
 [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
Fairly specific, but foreach (member; someStruct.tupleof). Part of my work on my IRC bot has been to serialise structs into configuration files (since std.json was *non-trivial* to deal with), and foreaching a someStruct.tupleof or a __traits(allMembers, symbol) allows for really, really interesting stuff.
Jun 23 2017
next sibling parent "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Fri, Jun 23, 2017 at 06:51:13PM +0000, Anonymouse via Digitalmars-d wrote:
[...]
 Fairly specific, but foreach (member; someStruct.tupleof).
 
 Part of my work on my IRC bot has been to serialise structs into
 configuration files (since std.json was *non-trivial* to deal with),
 and foreaching a someStruct.tupleof or a __traits(allMembers, symbol)
 allows for really, really interesting stuff.
Yup! Recently I wrote a module that, given a struct S annotated with certain UDA's, automatically: 1) Fills an instance of S with values extracted from a given range of key/value pairs (with automatic type conversion from string input to the correct field type via the awesome std.conv.to); 2) Displays a usage message listing the name of each field in S, its type, along with a message explaining that the field is for (the message is in a UDA attached to the struct field); 3) Given an instance of S, produces a listing of field names to the current values in the given instance. Put together, these 3 things can basically replace getopt(): you just declare a struct with UDAs attaching descriptions to struct fields, then just pass the program arguments along to this module and you're all set. It will parse the program arguments and fill in the struct for you, automatically generate a helpful usage message, and even dump the values of the obtained settings if you implement a verbose mode, for example. But it gets even better. Yesterday I added a new UDA that lets you split up your struct into multiple nested structs, but have all the field names visible at the top-level, i.e., say you have modules A, B, C, each declaring their own configuration structs: module A; struct Config { Desc("The number of iterations") int x; } module B; struct Config { Desc("The starting value") float y; } module B; struct Config { Desc("The output label") string z; } Then you can combine all of these configuration parameters into a single struct in your main program: struct Config { Denest A.Config aCfg; Denest B.Config bCfg; Denest C.Config cCfg; } The Denest UDA tells the parser that aCfg.x should appear to the user as simply "x", bCfg.y as just "y", and cCfg.z as "z". So the user doesn't have to know how your program is internally divided into modules; all of the settings appear as top-level settings. But internally, you pass Config.aCfg to module A, Config.bCfg to module B, etc., thereby avoiding leaking details about module B to module A, which would happen if you put everything into a single struct (you might end up accidentally having code in module A depend on a setting that belongs to module B, which breaks encapsulation and makes the code harder to maintain). D rawkz, I tell ya. T -- VI = Visual Irritation
Jun 23 2017
prev sibling parent Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Friday, 23 June 2017 at 18:51:13 UTC, Anonymouse wrote:
 On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
 Hi,

 I am currently trying to modernize the D code example roulette 
 on the dlang.org front page [1]. Hence, I would love to hear 
 about your favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"

 Before you ask, yes - I want to add a couple of cool examples 
 to dlang.org (and yep the roulette rotation is currently 
 broken [2]).

 [1] 
 https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
 [2] https://github.com/dlang/dlang.org/pull/1757
Fairly specific, but foreach (member; someStruct.tupleof). Part of my work on my IRC bot has been to serialise structs into configuration files (since std.json was *non-trivial* to deal with), and foreaching a someStruct.tupleof or a __traits(allMembers, symbol) allows for really, really interesting stuff.
Agreed. I have proposed [0] a variation on one of Sebastian's examples that uses that technique, so feel free to chime in with concrete suggestions there or here on the forums. [0]: https://github.com/dlang/dlang.org/pull/1762#issuecomment-310377649
Jun 24 2017
prev sibling parent "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Thu, Jun 22, 2017 at 12:48:25AM +0000, Seb via Digitalmars-d wrote:
 Hi,
 
 I am currently trying to modernize the D code example roulette on the
 dlang.org front page [1]. Hence, I would love to hear about your
 favorite feature(s) in D.
 Ideas:
 - favorite language construct
 - favorite code sample
 - "only possible in D"
[...] Today, I just got another idea for this: show off std.parallelism, especially parallel foreach!!! Here's the background: I have a program that takes user input, converts it into D code using a code template, invokes dmd to compile it into a shared library, and then loads the shared library so that the main program can call the compiled code directly. The output is a series of images that can be assembled into an animation. The first version of the code looked like this (greatly simplified, of course): void main(string[] args) { auto input = parseUserInput(args); auto libhandle = compile(input); foreach (i; 0 .. n) { auto outfile = File(...); ... // do stuff libhandle(...); // call compiled code ... // more stuff outfile.rawWrite(image); } } When n is large, of course, this runs rather slowly, because it's generating the images one by one. Parallel foreach comes to the rescue: void main(string[] args) { auto input = parseUserInput(args); auto libhandle = compile(input); foreach (i; parallel(iota(0, n))) { // *** auto outfile = File(...); ... // do stuff libhandle(...); // call compiled code ... // more stuff outfile.rawWrite(image); } } The only change is marked in // *** above. It's just a 1-line change, and boom, now the loop automatically spawns worker threads and generates the images in parallel. Instant 6x speedup on my AMD Hexacore machine! This all sounds very obvious, but actually there are some D-specific features at play here that make this even remotely possible: - First of all, I deliberately described the shared library bit, because at first I wasn't sure if std.parallel would Just Work(tm), the reason being that the shared object has global variables, and the loop body does change the values of those global variables. After reading the warning about implicit data sharing between threads in the docs, I was a bit wary of writing the loop as a parallel loop to begin with. Thankfully, TLS comes to the rescue: because the shared object is compiled as D code, the globals sit in the TLS segment, rather than C/C++'s default global storage. So modifying those globals turned out to be completely harmless w.r.t. parallel foreach: each thread would have its own copy of the global, so they don't interfere with each other. More importantly, this holds not only for the main program, but also for the *shared library* loaded via dlopen(). (It was this last part that I wasn't 100% sure about; it was already obvious that if these globals were in the main program, it would be no problem by default. But for this to also extend to manually-loaded shared libraries was a big relief for me.) That was the most tricky bit, but then other D features come together to make it possible to change a linear loop into a parallel loop by basically just editing the foreach statement: - opApply: in spite of being the scorned illegitimate second cousin of the beloved range-based counterparts, this is what made it possible to keep the only difference between a "native" foreach and a parallel foreach just a matter of a simple syntactic change in the foreach aggregate (just add a call to `parallel`). Without this, we'd have to hoist the loop body out into a delegate or some such, and replace the foreach with an awkward call to some obscure function in std.parallel. Not horrible, but just lots of fussy little editing details to take care of. Thanks to opApply, all we needed to do was to essentially add `parallel` to the one line. The compiler takes care of converting the loop body into the requisite delegate, transparently close over the required local variables, etc.. No fuss, no muss. - iota: the underappreciated construct, without which we'd have to manually construct some indexing array or some other such thing for parallel() to work with. I.e., a lot more tedious typing than just a simple 1-line change. - Finally, parallel() itself: one of the most ingenious API designs I've ever seen, it nicely encapsulates the dirty details of managing TaskPools manually, spawning worker threads, assigning Tasks to threads, synchronizing with threads afterwards, and all of that fussy boilerplate that one has to drown in if doing this in a language like C or C++. Thanks to all of the above, D lets you switch from a linear loop to a multithreaded loop just by changing 1 line of code. I don't know of any other imperative language that can boast this. (Of course, functional languages, esp. the pure ones, have this "by default". But we're talking about a dirty, mutating, imperative language with loop bodies that modify *global* variables -- to do this in spite of said mutation of globals, now *that's* an achievement.) T -- English is useful because it is a mess. Since English is a mess, it maps well onto the problem space, which is also a mess, which we call reality. Similarly, Perl was designed to be a mess, though in the nicest of all possible ways. -- Larry Wall
Jun 23 2017