digitalmars.D - Idiomatic D
- NoUseForAName (10/10) Jan 10 2014 I want to implement a small program in D for a personal
- Paulo Pinto (5/15) Jan 10 2014 A good starting point would be bearophile's work on Rosetta Code
- qznc (4/14) Jan 10 2014 Here is some:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/5) Jan 10 2014 I like this style:
- Brad Anderson (21/31) Jan 10 2014 The first thing that comes to made is learn ranges and slices.
- Kelet (63/73) Jan 10 2014 I'm pretty new to D myself, but I always strive to write
- Kelet (3/4) Jan 10 2014 Oops, meant
- qznc (6/9) Jan 10 2014 Contributing to Rosetta Code is a great way to learn idiomatic
- deadalnix (3/13) Jan 10 2014 Idiomatic D is when you hit compiler errors :D
- NoUseForAName (2/3) Jan 10 2014 Wow, that is a lot of information to process. Thanks everyone!
I want to implement a small program in D for a personal comparison of various programming languages. Given that I am already quite familiar with C/C++ (from K&R C to C++11) I could probably start write working D code right away by just looking up a few basic syntax/standard library things. However, I would like to write idiomatic D code, not "C++ in D". Is there any guide for this? If not, I would like to say that I think having one would be very useful. Assuming a lack of an extensive guide could someone please just give me the cliff notes on how to D properly?
Jan 10 2014
Am 10.01.2014 23:52, schrieb NoUseForAName:I want to implement a small program in D for a personal comparison of various programming languages. Given that I am already quite familiar with C/C++ (from K&R C to C++11) I could probably start write working D code right away by just looking up a few basic syntax/standard library things. However, I would like to write idiomatic D code, not "C++ in D". Is there any guide for this? If not, I would like to say that I think having one would be very useful. Assuming a lack of an extensive guide could someone please just give me the cliff notes on how to D properly?A good starting point would be bearophile's work on Rosetta Code http://rosettacode.org/wiki/Category:D -- Paulo
Jan 10 2014
On Friday, 10 January 2014 at 22:52:36 UTC, NoUseForAName wrote:I want to implement a small program in D for a personal comparison of various programming languages. Given that I am already quite familiar with C/C++ (from K&R C to C++11) I could probably start write working D code right away by just looking up a few basic syntax/standard library things. However, I would like to write idiomatic D code, not "C++ in D". Is there any guide for this? If not, I would like to say that I think having one would be very useful. Assuming a lack of an extensive guide could someone please just give me the cliff notes on how to D properly?Here is some: http://qznc.github.io/d-tut/idiomatic.html It is certainly not complete, though.
Jan 10 2014
On 01/10/2014 02:52 PM, NoUseForAName wrote:I would like to write idiomatic D code, not "C++ in D".I like this style: http://wiki.dlang.org/Component_programming_with_ranges Ali
Jan 10 2014
On Friday, 10 January 2014 at 22:52:36 UTC, NoUseForAName wrote:I want to implement a small program in D for a personal comparison of various programming languages. Given that I am already quite familiar with C/C++ (from K&R C to C++11) I could probably start write working D code right away by just looking up a few basic syntax/standard library things. However, I would like to write idiomatic D code, not "C++ in D". Is there any guide for this? If not, I would like to say that I think having one would be very useful. Assuming a lack of an extensive guide could someone please just give me the cliff notes on how to D properly?The first thing that comes to made is learn ranges and slices. Ranges: http://ddili.org/ders/d.en/ranges.html Slices: http://dlang.org/d-array-article.html UFCS (pseudo-member) chains particularly with std.algorithm and std.range are becoming something of a strong idiom in D. Contrived example: void main() { import std.algorithm, std.uni; auto result = "one two three four five" .splitter() .map!toUpper .filter!(a => a.canFind("E")); assert(result.equal(["ONE", "THREE", "FIVE"])); } Users of D generally focus more on compile time polymorphism rather than runtime where possible. Nick Sabalausky wrote a rather good article about this: http://www.semitwist.com/articles/EfficientAndFlexible/MultiplePages/Page1/
Jan 10 2014
On Friday, 10 January 2014 at 22:52:36 UTC, NoUseForAName wrote:I want to implement a small program in D for a personal comparison of various programming languages. Given that I am already quite familiar with C/C++ (from K&R C to C++11) I could probably start write working D code right away by just looking up a few basic syntax/standard library things. However, I would like to write idiomatic D code, not "C++ in D". Is there any guide for this? If not, I would like to say that I think having one would be very useful. Assuming a lack of an extensive guide could someone please just give me the cliff notes on how to D properly?I'm pretty new to D myself, but I always strive to write reasonably idiomatic code. So here's more or less what I've picked up: - Use type inference (auto) when possible - Use variable, function, and parameter qualifiers[1] - Use compile time function execution[2] - Constrain templates as much as possible - foreach or higher-order functions are preferred over for/while loops[3] - Do not overuse OOP[4] - Use uniform function call syntax when it makes sense to chain calls or increase readability - Know `alias this` and operator overloads - Put unit tests under each function if you choose to make them - Use Ddoc-style documentation - Many of the examples on Rosetta Code are well-written http://rosettacode.org/wiki/Category:D I think bearophile did many of them. [1]: Some Qualifiers to note - Function: - auto: infer return type - ref: returns a reference, not a value - inout: Deduce immutability (mutable, const, immutability) by parameters - pure: does not modify or use global state or impure functions - nothrow: No possibility to throw an exception - safe: No operation may corrupt memory - trusted – consider function safe, even if it cannot be verified so - system – default, all safe and trusted functions are also system Variable: - const: this reference to the variable is unchangeable - const is nice in function parameters because it can accept mutable, const, and immutable parameters - immutable: all references to the variable are unchangeable - shared: requires that variable is sharable between threads Parameter/Argument: - scope: no reference to variable can be leaked outside of function - in: equivalent to const scope - out: like ref, but gives default value, can be used for multiple returns - inout: see function qualifier definition - lazy: calculate only on usage (can duplicate calculations) Mostly, I find myself trying to use `pure safe nothrow` in functions, `immutable` in variables, and `in` in parameters as much as possible without modifying semantics too much. [2]: See last example here http://en.wikipedia.org/wiki/Compile_time_function_execution [3]: for(int i = 0; i < 20; i++) => foreach(immutable i = 0; 0..20) OR Use some higher-order function from std.algorithm or whatnot instead [4]: D is not Java. Modules can have their own constructors and private variables and such. Regards, Kelet
Jan 10 2014
On Friday, 10 January 2014 at 23:16:10 UTC, Kelet wrote:foreach(immutable i = 0; 0..20) OROops, meant foreach(immutable i; 0..20)
Jan 10 2014
On Friday, 10 January 2014 at 23:16:10 UTC, Kelet wrote:- Many of the examples on Rosetta Code are well-written http://rosettacode.org/wiki/Category:D I think bearophile did many of them.Contributing to Rosetta Code is a great way to learn idiomatic code. Write one of the missing programs [0] and tell bearophile about it. He will most certainly tell you how to make it more idiomatic and improve it. ;) [0] http://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_D
Jan 10 2014
On Friday, 10 January 2014 at 22:52:36 UTC, NoUseForAName wrote:I want to implement a small program in D for a personal comparison of various programming languages. Given that I am already quite familiar with C/C++ (from K&R C to C++11) I could probably start write working D code right away by just looking up a few basic syntax/standard library things. However, I would like to write idiomatic D code, not "C++ in D". Is there any guide for this? If not, I would like to say that I think having one would be very useful. Assuming a lack of an extensive guide could someone please just give me the cliff notes on how to D properly?Idiomatic D is when you hit compiler errors :D /sarcastic
Jan 10 2014
On Friday, 10 January 2014 at 22:52:36 UTC, NoUseForAName wrote:[snip]Wow, that is a lot of information to process. Thanks everyone!
Jan 10 2014