www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Idiomatic D?

reply "Matt Diesel" <M ttDiesel.co.uk> writes:
Today I learnt D. I have a lot of experience with other 
languages, but I think D fills a gap. So far I've really liked 

excited about some of the more powerful features.

I can teach myself a language no problem, but to make it useful 
it's always better to write it the way it was meant to be written.

Firstly, is there any good resource on what idiomatic D usage is? 
For instance Go has a huge page called Effective Go which tells 
you how you should use features, rather than what they are.

And secondly, I would be grateful if anyone could take a look at 
this very simple program and comment on how I'm using the 
language (ignoring the fact it's really simple and the lexer is 
rubbish). Just stuff like interface, class and variable naming 
and program structure.

Thanks a lot,

Matt
Jun 18 2012
next sibling parent reply "Matt Diesel" <M ttDiesel.co.uk> writes:
Sorry, forgot to post the code:

http://pastie.org/4109337

Is there no way to edit posts?
Jun 18 2012
parent Justin Whear <justin economicmodeling.com> writes:
On Mon, 18 Jun 2012 18:22:34 +0200, Matt Diesel wrote:

 Sorry, forgot to post the code:
 
 http://pastie.org/4109337
 
 Is there no way to edit posts?
A couple of observations: - According to the D style guide (http://dlang.org/dstyle.html), you should prefer to capitalize class and struct names, constants, and template parameters while starting functions and variables lowercase. in that you capitalize your method names. Obviously this is a more subjective thing, but your code will blend better with the standard library if you follow the guide. - Read up on ranges. I've written a number of lexers in the past few years in D and the lazy range idiom works really great for this use case. The basic idea is to model the lexer as a range of tokens which the parser (or anything else) can consume. Turns out you can composite a parser, syntax highlighter, etc. with map/reduce and some help from std.functional. The result is super flexible and can potentially be parallelized (your mileage may vary). Overall your code was pretty easy to follow and seemed fairly well written.
Jun 18 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Matt Diesel:

 Firstly, is there any good resource on what idiomatic D usage 
 is? For instance Go has a huge page called Effective Go which 
 tells you how you should use features, rather than what they 
 are.
I don't know any such page, but it looks like an interesting addition for the online docs.
 And secondly, I would be grateful if anyone could take a look 
 at this very simple program and comment on how I'm using the 
 language (ignoring the fact it's really simple and the lexer is 
 rubbish). Just stuff like interface, class and variable naming 
 and program structure.
Some notes: - In D method names start with a lowercase. - For multi-line ddoc comments there is also /** ... */ - Consider the usage of some structs, where appropriate. Structs - Consider adding pure/nothrow/const/in/mmutable (and even safe if you want) tags to methods, functions, arguments, and even normal variables, where possible. - this.input.length == 0 is better written this.input.empty, where empty for arrays (and strings) is in std.array. - Consider the usage of lambdas, to replace function int(int left, int right) { return left + right; } with (left, right) => left + right;. - Instead of free assert() consider using preconditions, postconditions and class/struct invariants. - I suggest to add some unittest{} blocks. - Instead of using Exception() consider creating one exception class for your code, or use one of the few standard ones... But I don't know how many standard ones there are in Phobos. - Overall you code looks well formatted, well commented, well written (I have not run it), it's a good start. Bye, bearophile
Jun 18 2012
prev sibling next sibling parent "Matt Diesel" <M ttDiesel.co.uk> writes:
Thanks to both of you, that was exactly the sort of input I was 
looking for :)

The D Style guide looks like what I wanted. It's more just about 
formatting but that should be enough for now.

I can't really reply to each individual point, but there is a lot 
of new stuff there that at first glance looks very useful. I'll 
reformat my code and have a play.

Thanks again.
Jun 18 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, June 18, 2012 18:20:58 Matt Diesel wrote:
 Today I learnt D. I have a lot of experience with other
 languages, but I think D fills a gap. So far I've really liked

 excited about some of the more powerful features.
 
 I can teach myself a language no problem, but to make it useful
 it's always better to write it the way it was meant to be written.
 
 Firstly, is there any good resource on what idiomatic D usage is?
 For instance Go has a huge page called Effective Go which tells
 you how you should use features, rather than what they are.
 
 And secondly, I would be grateful if anyone could take a look at
 this very simple program and comment on how I'm using the
 language (ignoring the fact it's really simple and the lexer is
 rubbish). Just stuff like interface, class and variable naming
 and program structure.
I haven't looked at your code at all, but probably the biggest concept that's used heavily in D (especially by the standard library) which is likely new to you is ranges (which are used rather than iterators). We need better documentation and articles about them in general, but probably the best tutorial currently available is this: http://ddili.org/ders/d.en/ranges.html - Jonathan M Davis
Jun 18 2012