arrays and foreach

Somedude lovelydear at mailmetrash.com
Wed Apr 18 01:26:18 PDT 2012


Le 17/04/2012 16:07, Ali Çehreli a écrit :
> On 04/17/2012 12:42 AM, Somedude wrote:
> 
>> Sorry for hijacking this thread, but since you're around, I hope you'll
>> see this message. As a D beginner, I'm browsing through your book.
>> I wanted to tell you that there is something essential missing in it:
>> how to compile. It's actually quite hard to find it in the official
>> website, and since the first chapters are targetted at complete
>> beginners, I thought it would be nice to add a few lines or pages about
>> compilation, linking, debugging, etc.
>> What do you think ?
> 
> Thank you very much for the feedback. I will do something about that.
> 
> Although there is no hello world compilation, the dmd command line
> appears multiple times throughout the chapters.
> 
> The 'assert and enforce' chapter shows it for the -release compiler
> switch and coincidentally I am on the 'Unit Testing' chapter as we
> speak, which includes the -unittest switch. :)
> 
> But you are right, both dmd and rdmd command lines must appear earlier.
> 
> Thank you,
> Ali
> 
Yes, I think that you have a lot of valuable information, but the
organization is lacking. The advanced chapters look good, but the first
"beginner" chapters can be .

what struck me as an error in your "Hello world program" chapter, you
give as an exercise:

"Type or copy the hello world program into your editor as a source file,
compile it, and run the program. The program should produce the
following output: Hello world!

If you are new to using a compiler, you will need to get help from the
compiler documentation or, preferably, an experienced person. You can't
benefit from the rest of this book if you can't compile and run programs."

Spock would have said: "Illogical, captain!" At this point, you haven't
told how to compile and run the program. Seriously, if your book is
aimed at complete beginners (and the first chapters seem to say that),
you really want to tell them what to do, not "go find the compiler
documentation", else if they can do that, they simply don't need to read
your book.

For a complete beginner, it's very frustrating to have to read 4
chapters of fluff, then write the program, and still not knowing how to
run the program. Where is the fun ? I can guarantee that at this point,
9 out of 10 beginners will drop the reading.


FIRST show, THEN explain. Better yet: first show, then ask questions,
and finally explain.

So how would I have organized this chapter ?
Something like this:

---------------
The first program to show in most programming...
Here is a hello world in D:
...

Copy this program and save it in a file under the name hello.d
.d is the extension for D source code.

Now we'll compile it, that is to say the compiler will check that the
syntax of this source code, and if it is valid D, will translate it into
machine language and make it a program.
Type in the command prompt:
rdmd hello.d

If you didn't make any mistake, you should have the impression that
nothing happened. In the contrary, everything went well ! You can check
that the executable file hello (or hello.exe under window) has been
created. If instead the compiler barked at you lots of text, you
probably made a mistake while copying. Correct it and retry. You will
routinely make many mistakes when programming so that this process of
correcting and compiling will get very familiar.

Once this is done, type the name of the executable to run it.
You should see
Hello, world !

Congratulations, your first D program is running !

------------

Then ONLY after that, I would proceed to the explanations.
(As an exercise, I would ask also: what is the smallest valid D program ?)

I would put the "Hello, world" chapter at the very beginning, right
after "The practice of programming".
This chapter would be enriched by an example on the computer, showing
the values.

The order of the beginning chapters I think should be:
Intro
Practice of programming
Hello world
writeln and write
Compiler
Fundamental types
I would then merge "Assignment and order of evaluation" with "Variables"

I would also remove the "Standard Input and Output Streams", they don't
belong to an introduction.
In fact, the problem is, you really want to separate your chapters into
a sections, one as an introductory course, and one for more advanced
explanations of the core language, and maybe one to introduce specific
concepts of the standard library (your last chapters).

I would put in Introduction section the first 19 chapters until
"Redirecting input and output", as well as all the flow constructs
(do-while, for, foreach, switch case, you really want to merge those
with other chapters like while, these don't need separate chapters),
ternary ops and associative arrays. With that, beginning programmers can
have some fun.

In the second section (core language), I would put
File
Program environment
auto typeof
name space
litterals
enum
function
function params
Lazy ops
Immutability
Classes <-- I didn't see a chapter on this topic ?
Intefaces
Exceptions
Scope
Assert and Enforce

In the third section (Generic Programming and advanced concepts), I
would put:
Templates
Mixins
Ranges
Parallelism
Concurrency
synchronized
__traits
calloc etc, or 2 chapters for interoperations with C/asm

Etc

Finally, as appendices, I would put chapters about the tools:
The Compiler <-- some more details about it
The Linker <-- name mangling, etc
Creating interface files, etc

I think this organization would make much more sense, and would make a
wonderful book.

I hope this helps.


More information about the Digitalmars-d-learn mailing list