digitalmars.D - Idioms you use
- Freddy (8/8) Sep 28 2015 Are any D idioms you use that you like to share?
- lobo (7/15) Sep 28 2015 What does this do and where would it be useful in my code?
- Cauterite (9/10) Sep 29 2015 I'm not sure if these fit under the definition of 'idiom', but
- anonymous (4/12) Sep 29 2015 Why not just `enum ctfe = 0xdead & 0xbad;`?
- Cauterite (5/9) Sep 29 2015 I'm pretty sure he's talking about the general use of an
- anonymous (2/3) Sep 29 2015 Buf of course! I totally didn't think of multiple statements. Thanks.
- Adam D. Ruppe (4/7) Sep 29 2015 If it is more complex than just one statement, putting it in a
- Marco Leise (26/34) Oct 03 2015 Yep, using that often, although I try to get my head around
- Artur Skawina via Digitalmars-d (6/46) Oct 03 2015 ==>
- Marco Leise (7/10) Oct 05 2015 Am Sun, 04 Oct 2015 00:08:39 +0200
- Artur Skawina via Digitalmars-d (7/17) Oct 05 2015 Note that I just wanted to show that the ctfeArr /function/ was not
Are any D idioms you use that you like to share? Heres one of mine --- enum ctfe = { return 0xdead & 0xbad; }(); ---
Sep 28 2015
On Monday, 28 September 2015 at 21:40:45 UTC, Freddy wrote:Are any D idioms you use that you like to share? Heres one of mine --- enum ctfe = { return 0xdead & 0xbad; }(); ---What does this do and where would it be useful in my code? For D idioms I usually go here...you may want to submit a PR: http://p0nce.github.io/d-idioms/ Phobos is another good source of how to do X in D. bye, lobo
Sep 28 2015
On Monday, 28 September 2015 at 21:40:45 UTC, Freddy wrote:Are any D idioms you use that you like to share?I'm not sure if these fit under the definition of 'idiom', but they sort of areā¦ I think. http://dpaste.dzfl.pl/f66a76a7411f You could even extend the concept with opDispatch to achieve syntax like this: pinvoke.psapi.GetProcessImageFileNameW!uint(...); I've used a similar technique (+ caching) to lazily load OpenGL extensions. It worked really well.
Sep 29 2015
On Monday 28 September 2015 23:40, Freddy wrote:Are any D idioms you use that you like to share? Heres one of mine --- enum ctfe = { return 0xdead & 0xbad; }(); ---Why not just `enum ctfe = 0xdead & 0xbad;`? Are there cases where `enum foo = {return bar;}();` works but `enum foo = bar;` doesn't? And if there are, aren't they compiler bugs?
Sep 29 2015
On Tuesday, 29 September 2015 at 12:52:36 UTC, anonymous wrote:Why not just `enum ctfe = 0xdead & 0xbad;`? Are there cases where `enum foo = {return bar;}();` works but `enum foo = bar;` doesn't? And if there are, aren't they compiler bugs?I'm pretty sure he's talking about the general use of an anonymous function invocation assigned to an enum to force some statements to be executed at compile time. His example was not very illustrative though.
Sep 29 2015
On Tuesday 29 September 2015 15:06, Cauterite wrote:some statementsBuf of course! I totally didn't think of multiple statements. Thanks.
Sep 29 2015
On Tuesday, 29 September 2015 at 12:52:36 UTC, anonymous wrote:Are there cases where `enum foo = {return bar;}();` works but `enum foo = bar;` doesn't? And if there are, aren't they compiler bugs?If it is more complex than just one statement, putting it in a function lets you execute multiple lines (including loops and stuff) at once.
Sep 29 2015
Am Mon, 28 Sep 2015 21:40:43 +0000 schrieb Freddy <Hexagonalstar64 gmail.com>:Are any D idioms you use that you like to share? Heres one of mine --- enum ctfe = { return 0xdead & 0xbad; }(); ---Yep, using that often, although I try to get my head around using functional style one-line expressions where possible to avoid the boilerplate. static immutable ctfe = { bool[256] result; foreach (i; 0 .. 256) result = isDigit(i); return result; }(); ==> static immutable bool[256] ctfe = iota(256).map!isDigit.array; ==> static ctfe = ctfeArr!( iota(256).map!isDigit ); enum ctfeArr(alias r)() { // r.length doesn't work as static array size enum length = r.length; // immutable doesn't work on this (cannot modify const) ElementType!(typeof(r))[length] result = r.array; // Cross fingers here ... return cast(immutable) result; } -- Marco
Oct 03 2015
On 10/03/15 15:53, Marco Leise via Digitalmars-d wrote:Am Mon, 28 Sep 2015 21:40:43 +0000 schrieb Freddy <Hexagonalstar64 gmail.com>:==> static ctfe = ctfeArr!( iota(256).map!isDigit() ); immutable typeof(R.front)[R.array().length] ctfeArr(alias R) = R.array(); [`array` is only required because of compiler issues, yes.] arturAre any D idioms you use that you like to share? Heres one of mine --- enum ctfe = { return 0xdead & 0xbad; }(); ---Yep, using that often, although I try to get my head around using functional style one-line expressions where possible to avoid the boilerplate. static immutable ctfe = { bool[256] result; foreach (i; 0 .. 256) result = isDigit(i); return result; }(); ==> static immutable bool[256] ctfe = iota(256).map!isDigit.array; ==> static ctfe = ctfeArr!( iota(256).map!isDigit ); enum ctfeArr(alias r)() { // r.length doesn't work as static array size enum length = r.length; // immutable doesn't work on this (cannot modify const) ElementType!(typeof(r))[length] result = r.array; // Cross fingers here ... return cast(immutable) result; }
Oct 03 2015
Am Sun, 04 Oct 2015 00:08:39 +0200 schrieb Artur Skawina via Digitalmars-d <digitalmars-d puremagic.com>:static ctfe = ctfeArr!( iota(256).map!isDigit() ); immutable typeof(R.front)[R.array().length] ctfeArr(alias R) = R.array();I like that. Also that 1) In D everything is possible. And 2) If not, there is a workaround, goto 1). -- Marco
Oct 05 2015
On 10/05/15 17:53, Marco Leise via Digitalmars-d wrote:Am Sun, 04 Oct 2015 00:08:39 +0200 schrieb Artur Skawina via Digitalmars-d <digitalmars-d puremagic.com>:Note that I just wanted to show that the ctfeArr /function/ was not necessary, and I didn't want that `ctfe` declaration to be affected; real code would look more like: enum typeof(R.front)[R.array().length] ctfeArr(alias R) = R.array(); immutable ctfe = ctfeArr!( iota(256).map!isDigit() ); arturstatic ctfe = ctfeArr!( iota(256).map!isDigit() ); immutable typeof(R.front)[R.array().length] ctfeArr(alias R) = R.array();I like that. Also that 1) In D everything is possible. And 2) If not, there is a workaround, goto 1).
Oct 05 2015