January 08, 2015 Re: D idioms list | ||||
---|---|---|---|---|
| ||||
Posted in reply to Foo | On Thursday, 8 January 2015 at 21:28:56 UTC, Foo wrote:
> On Thursday, 8 January 2015 at 20:00:11 UTC, Foo wrote:
>> 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 ;)
>
> And the public label is redundant.
Corrected.
Never realized public: was implied.
|
January 08, 2015 Re: D idioms list | ||||
---|---|---|---|---|
| ||||
Posted in reply to ponce | 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.
|
January 09, 2015 Re: D idioms list | ||||
---|---|---|---|---|
| ||||
Posted in reply to ponce | I saw recently (at last in this thread: http://forum.dlang.org/thread/tdfydchrairigdlgtyum@forum.dlang.org#post-qakiogaqvmiwlneimhgu: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); } ---- |
January 09, 2015 Re: D idioms list | ||||
---|---|---|---|---|
| ||||
Attachments: | 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: > > 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 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. |
January 09, 2015 Re: D idioms list | ||||
---|---|---|---|---|
| ||||
Posted in reply to ponce Attachments: | 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 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! 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". |
January 09, 2015 Re: D idioms list | ||||
---|---|---|---|---|
| ||||
Posted in reply to ponce Attachments: | 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 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! 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.com |
January 09, 2015 Re: D idioms list | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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.com
I didn't knew alias this does object slicing. Will add it.
|
January 10, 2015 Re: D idioms list | ||||
---|---|---|---|---|
| ||||
Posted in reply to ponce | 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) |
January 10, 2015 Re: D idioms list | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Saturday, 10 January 2015 at 20:37:04 UTC, Walter Bright wrote:
> 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)
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.
|
January 10, 2015 Re: D idioms list | ||||
---|---|---|---|---|
| ||||
Posted in reply to weaselcat | 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. |
Copyright © 1999-2021 by the D Language Foundation