digitalmars.D.announce - D idioms list
- ponce (9/9) Jan 08 2015 I've started a list of curated D tips and tricks here:
- uri (8/17) Jan 08 2015 This is great, thanks.
- ponce (5/12) Jan 08 2015 I'm not familiar with the terse, range-heavy, UFCS style that has
- uri (9/24) Jan 08 2015 No I admit I don't have any real list. It's always an "in the
- bearophile (10/12) Jan 08 2015 In Rosettacode I have inserted tons of examples of that coding
- ponce (4/16) Jan 08 2015 Cool. I will link to the Rosettacode D pages since I've used them
- Szymon Gatner (7/16) Jan 08 2015 They are really cool, thanks :)
- ketmar via Digitalmars-d-announce (5/29) Jan 08 2015 On Thu, 08 Jan 2015 11:24:34 +0000
- Szymon Gatner (4/34) Jan 08 2015 Ah, thanks. Follow up then: can such imported string be used for
- ponce (2/15) Jan 08 2015 Yes.
- Szymon Gatner (2/19) Jan 08 2015 That is pretty damn cool then.
- ketmar via Digitalmars-d-announce (8/44) Jan 08 2015 On Thu, 08 Jan 2015 11:41:42 +0000
- Robert burner Schadek (4/4) Jan 08 2015 that a really nice idea, thanks.
- Foo (3/12) Jan 08 2015 "Struct inheritance with alias this"
- Foo (2/18) Jan 08 2015 And the public label is redundant.
- Adam D. Ruppe (19/19) Jan 08 2015 A note:
- ponce (2/10) Jan 08 2015 Feel free to take and extend :)
- ketmar via Digitalmars-d-announce (8/21) Jan 08 2015 On Thu, 08 Jan 2015 10:21:25 +0000
- ponce (3/11) Jan 08 2015 Renamed, thanks!
- ketmar via Digitalmars-d-announce (6/18) Jan 08 2015 On Thu, 08 Jan 2015 21:22:30 +0000
- ketmar via Digitalmars-d-announce (8/20) Jan 08 2015 On Thu, 08 Jan 2015 21:22:30 +0000
- ponce (3/10) Jan 09 2015 I didn't knew alias this does object slicing. Will add it.
- Artur Skawina via Digitalmars-d-announce (14/19) Jan 08 2015 'alias this' is just the D syntax for implicit conversions.
- weaselcat (4/13) Jan 08 2015 Not much to add but I enjoy reading 'idiomatic' D content -
- ketmar via Digitalmars-d-announce (6/30) Jan 08 2015 On Thu, 08 Jan 2015 22:25:11 +0100
- Walter Bright (4/8) Jan 10 2015 My contribution:
- weaselcat (4/14) Jan 10 2015 Sorry for the off-topic noise, but where will you be publishing
- Walter Bright (4/7) Jan 10 2015 It's a good question. Dr. Dobb's has graciously given me permission to r...
- Russel Winder via Digitalmars-d-announce (9/21) Jan 11 2015 Feel free to send stuff to ACCU's CVu or Overload.
- Walter Bright (2/4) Jan 11 2015 Good idea!
- Vlad Levenfeld (4/4) Jan 14 2015 For optimal AA lookup, this idiom is also nice if you only need
- ponce (7/11) Jan 15 2015 Having a declaration in an "if" could be another entry together
- Muhavare (5/5) Mar 08 2016 For optimal AA lookup, this idiom is also nice if you only need
I've started a list of curated D tips and tricks here: http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in the D world is welcome to be added or suggested. I think the focus should be on "stuff that could make you more productive, or is just funky" but that is up to debate. Of course the D Cookbook still stays irreplaceable for a consistent, in-depth discussion of being D-enabled. Thoughts?
Jan 08 2015
On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:I've started a list of curated D tips and tricks here: http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in the D world is welcome to be added or suggested. I think the focus should be on "stuff that could make you more productive, or is just funky" but that is up to debate. Of course the D Cookbook still stays irreplaceable for a consistent, in-depth discussion of being D-enabled. Thoughts?This is great, thanks. Something I personally would find useful is a comparison between the C++ way and idiomatic D with Phobos. I finding coming from C/C++ to D very easy but I'm always wondering if I'm doing things the "D" way. Cheers, uri
Jan 08 2015
On Thursday, 8 January 2015 at 10:30:38 UTC, uri wrote:This is great, thanks. Something I personally would find useful is a comparison between the C++ way and idiomatic D with Phobos. I finding coming from C/C++ to D very easy but I'm always wondering if I'm doing things the "D" way. Cheers, uriI'm not familiar with the terse, range-heavy, UFCS style that has emerged from Phobos so I'm not sure if I can write that. What could help is a list of tasks for which you asked yourself what the "D" way was. Is there one?
Jan 08 2015
On Thursday, 8 January 2015 at 10:35:07 UTC, ponce wrote:On Thursday, 8 January 2015 at 10:30:38 UTC, uri wrote:No I admit I don't have any real list. It's always an "in the moment" sort of thing and I then just choose a D-ish/C++ style and promptly forget the exact details. I'll start to compile a list each time this comes up. And if I find any good D idioms in the process I'll include them in the list as well. Thanks, uriThis is great, thanks. Something I personally would find useful is a comparison between the C++ way and idiomatic D with Phobos. I finding coming from C/C++ to D very easy but I'm always wondering if I'm doing things the "D" way. Cheers, uriI'm not familiar with the terse, range-heavy, UFCS style that has emerged from Phobos so I'm not sure if I can write that. What could help is a list of tasks for which you asked yourself what the "D" way was. Is there one?
Jan 08 2015
ponce:I'm not familiar with the terse, range-heavy, UFCS style that has emerged from PhobosIn Rosettacode I have inserted tons of examples of that coding style. An example, given a tuple of arbitrary length, with items all of the same type, how do you compute the total of its items? The last way I've invented is: myTuple[].only.sum It's also nogc. But it causes a little of template bloat. Bye, bearophile
Jan 08 2015
On Thursday, 8 January 2015 at 10:56:00 UTC, bearophile wrote:ponce:Cool. I will link to the Rosettacode D pages since I've used them in the past when time-constrained, especially all things regarding text files.I'm not familiar with the terse, range-heavy, UFCS style that has emerged from PhobosIn Rosettacode I have inserted tons of examples of that coding style. An example, given a tuple of arbitrary length, with items all of the same type, how do you compute the total of its items? The last way I've invented is: myTuple[].only.sum It's also nogc. But it causes a little of template bloat. Bye, bearophile
Jan 08 2015
On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:I've started a list of curated D tips and tricks here: http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in the D world is welcome to be added or suggested. I think the focus should be on "stuff that could make you more productive, or is just funky" but that is up to debate. Of course the D Cookbook still stays irreplaceable for a consistent, in-depth discussion of being D-enabled. Thoughts?They are really cool, thanks :) Question: Where did this syntax came from? It is not documented for 'import' keyword.(first time I see that D has built-in resource compiler): ubyte[] sdlBytes = cast(ubyte[]) import("SDL2.dll");
Jan 08 2015
On Thu, 08 Jan 2015 11:24:34 +0000 Szymon Gatner via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> wrote:On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:it is documented: http://dlang.org/expression.html#ImportExpression it's a nice D habit of overloading keywords.I've started a list of curated D tips and tricks here:=20 http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in=20 the D world is welcome to be added or suggested. I think the focus should be on "stuff that could make you more=20 productive, or is just funky" but that is up to debate. Of course the D Cookbook still stays irreplaceable for a=20 consistent, in-depth discussion of being D-enabled. Thoughts?=20 They are really cool, thanks :) =20 Question: =20 Where did this syntax came from? It is not documented for=20 'import' keyword.(first time I see that D has built-in resource=20 compiler): =20 ubyte[] sdlBytes =3D cast(ubyte[]) import("SDL2.dll");
Jan 08 2015
On Thursday, 8 January 2015 at 11:31:14 UTC, ketmar via Digitalmars-d-announce wrote:On Thu, 08 Jan 2015 11:24:34 +0000 Szymon Gatner via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> wrote:Ah, thanks. Follow up then: can such imported string be used for mixin?On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:it is documented: http://dlang.org/expression.html#ImportExpression it's a nice D habit of overloading keywords.I've started a list of curated D tips and tricks here: http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in the D world is welcome to be added or suggested. I think the focus should be on "stuff that could make you more productive, or is just funky" but that is up to debate. Of course the D Cookbook still stays irreplaceable for a consistent, in-depth discussion of being D-enabled. Thoughts?They are really cool, thanks :) Question: Where did this syntax came from? It is not documented for 'import' keyword.(first time I see that D has built-in resource compiler): ubyte[] sdlBytes = cast(ubyte[]) import("SDL2.dll");
Jan 08 2015
On Thursday, 8 January 2015 at 11:41:43 UTC, Szymon Gatner wrote:Yes.Ah, thanks. Follow up then: can such imported string be used for mixin?Question: Where did this syntax came from? It is not documented for 'import' keyword.(first time I see that D has built-in resource compiler): ubyte[] sdlBytes = cast(ubyte[]) import("SDL2.dll");it is documented: http://dlang.org/expression.html#ImportExpression it's a nice D habit of overloading keywords.
Jan 08 2015
On Thursday, 8 January 2015 at 11:43:30 UTC, ponce wrote:On Thursday, 8 January 2015 at 11:41:43 UTC, Szymon Gatner wrote:That is pretty damn cool then.Yes.Ah, thanks. Follow up then: can such imported string be used for mixin?Question: Where did this syntax came from? It is not documented for 'import' keyword.(first time I see that D has built-in resource compiler): ubyte[] sdlBytes = cast(ubyte[]) import("SDL2.dll");it is documented: http://dlang.org/expression.html#ImportExpression it's a nice D habit of overloading keywords.
Jan 08 2015
On Thu, 08 Jan 2015 11:41:42 +0000 Szymon Gatner via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> wrote:On Thursday, 8 January 2015 at 11:31:14 UTC, ketmar via=20 Digitalmars-d-announce wrote:sure. either directly, or you can use CTFE to parse imported data and generate code. for now it's somewhat limited, 'cause CTFE parsing eats alot of memory, but when we'll have 128GB of RAM at bare minimum... i don't think that i'll be using external preprocessors to generate D code from various text and binary files.On Thu, 08 Jan 2015 11:24:34 +0000 Szymon Gatner via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> wrote:=20 Ah, thanks. Follow up then: can such imported string be used for=20 mixin?On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:it is documented:=20 http://dlang.org/expression.html#ImportExpression it's a nice D habit of overloading keywords.I've started a list of curated D tips and tricks here:=20 http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in=20 the D world is welcome to be added or suggested. I think the focus should be on "stuff that could make you=20 more productive, or is just funky" but that is up to debate. Of course the D Cookbook still stays irreplaceable for a=20 consistent, in-depth discussion of being D-enabled. Thoughts?=20 They are really cool, thanks :) =20 Question: =20 Where did this syntax came from? It is not documented for=20 'import' keyword.(first time I see that D has built-in=20 resource compiler): =20 ubyte[] sdlBytes =3D cast(ubyte[]) import("SDL2.dll");
Jan 08 2015
that a really nice idea, thanks. substring position, std.string.(last)indexOf(|Any|Neither) may be better btw. this should move to the dlang wiki. Any takers?
Jan 08 2015
On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:I've started a list of curated D tips and tricks here: http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in the D world is welcome to be added or suggested. I think the focus should be on "stuff that could make you more productive, or is just funky" but that is up to debate. Of course the D Cookbook still stays irreplaceable for a consistent, in-depth discussion of being D-enabled. Thoughts?"Struct inheritance with alias this" You are using a class ;)
Jan 08 2015
On Thursday, 8 January 2015 at 20:00:11 UTC, Foo wrote:On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:And the public label is redundant.I've started a list of curated D tips and tricks here: http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in the D world is welcome to be added or suggested. I think the focus should be on "stuff that could make you more productive, or is just funky" but that is up to debate. Of course the D Cookbook still stays irreplaceable for a consistent, in-depth discussion of being D-enabled. Thoughts?"Struct inheritance with alias this" You are using a class ;)
Jan 08 2015
On Thursday, 8 January 2015 at 21:28:56 UTC, Foo wrote:On Thursday, 8 January 2015 at 20:00:11 UTC, Foo wrote:Corrected. Never realized public: was implied.On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:And the public label is redundant.I've started a list of curated D tips and tricks here: http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in the D world is welcome to be added or suggested. I think the focus should be on "stuff that could make you more productive, or is just funky" but that is up to debate. Of course the D Cookbook still stays irreplaceable for a consistent, in-depth discussion of being D-enabled. Thoughts?"Struct inheritance with alias this" You are using a class ;)
Jan 08 2015
I saw recently (at last in this thread: http://forum.dlang.org/thread/tdfydchrairigdlgtyum forum.dlang.org#post-qakiogaqvmiwlneimhg :40forum.dlang.org) that many users use ---- key in aa ? aa[key] : ValueType.init; ---- instead of ---- auto ptr = key in aa; ptr ? *ptr : ValueType.init; ---- which is more economic. Maybe you can add it to your list: ---- import std.stdio; void main() { immutable string key = "foo"; immutable string[string] arr = [key : "bar"]; if (auto ptr = key in arr) writeln(*ptr); } ----
Jan 08 2015
A note: a resource compiler is still useful for things like giving a Windows program an icon or other such things where the operating system needs to find the data, since the OS doesn't know how D stores info. (a fun project that I'm kinda tempted to do some day btw: make program that CAN fetch imported data from a D program. Maybe reading the executable, or maybe establishing a command line protocol to fetch it with the program's help. ) You can compile resources on Windows in D exactly the same way as in C. Another thing too, I'm starting work on a "This week in D" thing and tips like these are one of the things I'd like to put in it. I guess I don't have anything else to say about it right now, I still have some more prep work to do, but if any of you have idioms or tricks, we should compile a list for reference and perhaps slightly longer articles about their use we can stick in the newsletter. (I'm thinking the length should only be 2-5 paragraphs.)
Jan 08 2015
On Thursday, 8 January 2015 at 20:16:24 UTC, Adam D. Ruppe wrote:Another thing too, I'm starting work on a "This week in D" thing and tips like these are one of the things I'd like to put in it. I guess I don't have anything else to say about it right now, I still have some more prep work to do, but if any of you have idioms or tricks, we should compile a list for reference and perhaps slightly longer articles about their use we can stick in the newsletter. (I'm thinking the length should only be 2-5 paragraphs.)Feel free to take and extend :)
Jan 08 2015
On Thu, 08 Jan 2015 10:21:25 +0000 ponce via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> wrote:I've started a list of curated D tips and tricks here:=20 http://p0nce.github.io/d-idioms/ =20 Anything that you wished you learned earlier at one point in the=20 D world is welcome to be added or suggested. =20 I think the focus should be on "stuff that could make you more=20 productive, or is just funky" but that is up to debate. =20 Of course the D Cookbook still stays irreplaceable for a=20 consistent, in-depth discussion of being D-enabled. =20 Thoughts?i'm not sure, but maybe it worth renaming "struct inheritance" to "extending a struct"? or even something completely different. what it does is actually extending/augmenting the struct, but not OO-inheritance, as one cannot pass "augmented" struct to the function which expects original struct. at least without hackery.
Jan 08 2015
On Thursday, 8 January 2015 at 20:23:11 UTC, ketmar via Digitalmars-d-announce wrote:i'm not sure, but maybe it worth renaming "struct inheritance" to "extending a struct"? or even something completely different. what it does is actually extending/augmenting the struct, but not OO-inheritance, as one cannot pass "augmented" struct to the function which expects original struct. at least without hackery.Renamed, thanks!
Jan 08 2015
On Thu, 08 Jan 2015 21:22:30 +0000 ponce via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> wrote:On Thursday, 8 January 2015 at 20:23:11 UTC, ketmar via=20 Digitalmars-d-announce wrote:we actually can pass "extended" struct as original one, as Artur shown, but i believe that "extending" is still better. p.s. you forgot to fix TOC, which still reads "struct inheritance".i'm not sure, but maybe it worth renaming "struct inheritance"=20 to "extending a struct"? or even something completely different.=20 what it does is actually extending/augmenting the struct, but not OO-inheritance, as one cannot pass "augmented" struct to the=20 function which expects original struct. at least without hackery.=20 Renamed, thanks!
Jan 08 2015
On Thu, 08 Jan 2015 21:22:30 +0000 ponce via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> wrote:On Thursday, 8 January 2015 at 20:23:11 UTC, ketmar via=20 Digitalmars-d-announce wrote:p.p.s. maybe it's worth adding Artur's code sample[1] too, to show that "extended" structure can be passed to functions which requires original one? it's not obvious, at least for me. ;-) [1] http://forum.dlang.org/post/mailman.4332.1420752329.9932.digitalmars-d-= announce puremagic.comi'm not sure, but maybe it worth renaming "struct inheritance"=20 to "extending a struct"? or even something completely different.=20 what it does is actually extending/augmenting the struct, but not OO-inheritance, as one cannot pass "augmented" struct to the=20 function which expects original struct. at least without hackery.=20 Renamed, thanks!
Jan 08 2015
On Friday, 9 January 2015 at 05:58:09 UTC, ketmar via Digitalmars-d-announce wrote:p.p.s. maybe it's worth adding Artur's code sample[1] too, to show that "extended" structure can be passed to functions which requires original one? it's not obvious, at least for me. ;-) [1] http://forum.dlang.org/post/mailman.4332.1420752329.9932.digitalmars-d-announce puremagic.comI didn't knew alias this does object slicing. Will add it.
Jan 09 2015
On 01/08/15 21:23, ketmar via Digitalmars-d-announce wrote:i'm not sure, but maybe it worth renaming "struct inheritance" to "extending a struct"? or even something completely different. what it does is actually extending/augmenting the struct, but not OO-inheritance, as one cannot pass "augmented" struct to the function which expects original struct. at least without hackery.'alias this' is just the D syntax for implicit conversions. The feature /is/ crippled, but there's no need for "hackery"; at least not for simple things like that. struct A { int a; } struct B { A a; alias a this; string b; } int f(A a) { return a.a+1; } int g(ref A a) { return a.a+1; } ref A h(ref A a) { return a; } int main() { B b; return f(b)+g(b)+h(b).a; } artur
Jan 08 2015
On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:I've started a list of curated D tips and tricks here: http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in the D world is welcome to be added or suggested. I think the focus should be on "stuff that could make you more productive, or is just funky" but that is up to debate. Of course the D Cookbook still stays irreplaceable for a consistent, in-depth discussion of being D-enabled. Thoughts?Not much to add but I enjoy reading 'idiomatic' D content - coming from C++, I feel like I'm often not writing my D code like I should be. Thanks for the extra resource.
Jan 08 2015
On Thu, 08 Jan 2015 22:25:11 +0100 Artur Skawina via Digitalmars-d-announce <digitalmars-d-announce puremagic.com> wrote:On 01/08/15 21:23, ketmar via Digitalmars-d-announce wrote:mea culpa. i completely forgot about that feature of `alias this`, and was pretty sure that the code above is invalid. i never bothered to really check it. sorry.i'm not sure, but maybe it worth renaming "struct inheritance" to "extending a struct"? or even something completely different. what it does is actually extending/augmenting the struct, but not OO-inheritance, as one cannot pass "augmented" struct to the function which expects original struct. at least without hackery.=20 'alias this' is just the D syntax for implicit conversions. The feature /is/ crippled, but there's no need for "hackery"; at least not for simple things like that. =20 struct A { int a; } struct B { A a; alias a this; string b; } =20 int f(A a) { return a.a+1; } int g(ref A a) { return a.a+1; } ref A h(ref A a) { return a; } =20 int main() { B b; return f(b)+g(b)+h(b).a; } =20 artur
Jan 08 2015
On 1/8/2015 2:21 AM, ponce wrote:I've started a list of curated D tips and tricks here: http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in the D world is welcome to be added or suggested.My contribution: http://digitalmars.com/articles/b68.html (Member function pointers in D)
Jan 10 2015
On Saturday, 10 January 2015 at 20:37:04 UTC, Walter Bright wrote:On 1/8/2015 2:21 AM, ponce wrote:Sorry for the off-topic noise, but where will you be publishing your articles since Dr.Dobbs has closed? Sorry if you have answered this elsewhere.I've started a list of curated D tips and tricks here: http://p0nce.github.io/d-idioms/ Anything that you wished you learned earlier at one point in the D world is welcome to be added or suggested.My contribution: http://digitalmars.com/articles/b68.html (Member function pointers in D)
Jan 10 2015
On 1/10/2015 1:28 PM, weaselcat wrote:Sorry for the off-topic noise, but where will you be publishing your articles since Dr.Dobbs has closed? Sorry if you have answered this elsewhere.It's a good question. Dr. Dobb's has graciously given me permission to republish them, and I'll post them on http://digitalmars.com/articles. As you can see, I've already done a few of them. Lots more to go.
Jan 10 2015
On Sat, 2015-01-10 at 14:13 -0800, Walter Bright via Digitalmars-d-announce wrote:On 1/10/2015 1:28 PM, weaselcat wrote:Feel free to send stuff to ACCU's CVu or Overload. http://accu.org/index.php/journal -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winderSorry for the off-topic noise, but where will you be publishing your articles since Dr.Dobbs has closed? Sorry if you have answered this elsewhere.It's a good question. Dr. Dobb's has graciously given me permission to republish them, and I'll post them on http://digitalmars.com/articles. As you can see, I've already done a few of them. Lots more to go.
Jan 11 2015
On 1/11/2015 4:24 AM, Russel Winder via Digitalmars-d-announce wrote:Feel free to send stuff to ACCU's CVu or Overload. http://accu.org/index.php/journalGood idea!
Jan 11 2015
For optimal AA lookup, this idiom is also nice if you only need the result for one line: if (auto found = key in AA) do_stuff (found);
Jan 14 2015
On Thursday, 15 January 2015 at 06:02:13 UTC, Vlad Levenfeld wrote:For optimal AA lookup, this idiom is also nice if you only need the result for one line: if (auto found = key in AA) do_stuff (found);Having a declaration in an "if" could be another entry together with: if (auto inst = cast(SubClass)myObject) do_stuff(inst); How to do "instanceof" is quite a common question on IRC.
Jan 15 2015
For optimal AA lookup, this idiom is also nice if you only need the result for one line: if (auto found = key in AA) do_stuff (found); http://idioms.in/
Mar 08 2016