View mode: basic / threaded / horizontal-split · Log in · Help
April 16, 2012
arrays and foreach
i have this example program:

---
void main()
{
    int[3] a;
    foreach (p; a)
        p = 42;
    writeln(a);
}
---

after running it, i expect to get [42, 42, 42] but instead i get 
[0, 0, 0] (i know that you can do a[] = 42, it's just a trivial 
example). So it seems that you cannot write into the array 
because the elements are being passed by value each iteration. It 
possible to have 'p' passed by reference?
April 17, 2012
Re: arrays and foreach
On 4/17/12, darkstalker <slayerbeast@gmail.com> wrote:
>  It possible to have 'p' passed by reference?

Yes and the answer is in the question:

foreach (ref p; a)
   p = 42;
April 17, 2012
Re: arrays and foreach
On 04/16/2012 04:56 PM, darkstalker wrote:
> i have this example program:
>
> ---
> void main()
> {
> int[3] a;
> foreach (p; a)
> p = 42;
> writeln(a);
> }
> ---
>
> after running it, i expect to get [42, 42, 42] but instead i get [0, 0,
> 0] (i know that you can do a[] = 42, it's just a trivial example). So it
> seems that you cannot write into the array because the elements are
> being passed by value each iteration. It possible to have 'p' passed by
> reference?

Yes:

    foreach (ref p; a)

Ali
April 17, 2012
Re: arrays and foreach
On Tuesday, 17 April 2012 at 00:00:57 UTC, Andrej Mitrovic wrote:
> On 4/17/12, darkstalker <slayerbeast@gmail.com> wrote:
>>  It possible to have 'p' passed by reference?
>
> Yes and the answer is in the question:
>
> foreach (ref p; a)
>     p = 42;

thanks, it works
April 17, 2012
Re: arrays and foreach
Le 17/04/2012 02:01, Ali Çehreli a écrit :
> On 04/16/2012 04:56 PM, darkstalker wrote:
>> i have this example program:
>>
>> ---
>> void main()
>> {
>> int[3] a;
>> foreach (p; a)
>> p = 42;
>> writeln(a);
>> }
>> ---
>>
>> after running it, i expect to get [42, 42, 42] but instead i get [0, 0,
>> 0] (i know that you can do a[] = 42, it's just a trivial example). So it
>> seems that you cannot write into the array because the elements are
>> being passed by value each iteration. It possible to have 'p' passed by
>> reference?
> 
> Yes:
> 
>     foreach (ref p; a)
> 
> Ali
> 
> 
Hi Ali,

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 ?
April 17, 2012
Compiling Was: arrays and foreach
On 4/17/2012 4:42 PM, Somedude wrote:

>>
>> Ali
>>
>>
> Hi Ali,
>
> 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 ?

In the sidebar at dlang.org, under Documentation, is a link labeled 
"Tutorial". The very first code example shows a minimal program and how 
to compile it.
April 17, 2012
Re: Compiling Was: arrays and foreach
Le 17/04/2012 12:19, Mike Parker a écrit :
> On 4/17/2012 4:42 PM, Somedude wrote:
> 
>>>
>>> Ali
>>>
>>>
>> Hi Ali,
>>
>> 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 ?
> 
> In the sidebar at dlang.org, under Documentation, is a link labeled
> "Tutorial". The very first code example shows a minimal program and how
> to compile it.

Well, yes, that's sufficient for "Hello, world", but not for something
barely more complex, i.e a function with a unit test.

In this case, I had to type:
rdmd -unittest --main test.d

Without the --main, I would get linker errors, and couldn't find the
reason for these errors. Happily, someone here explained me that the
effect of the --main flag was to insert a main() function just for this
case.

Now, I see there is a mention of this in http://dlang.org/rdmd.html
But without rdmd, I would compile with dmd, which has no --main switch,
and it would still fail to link, because of lack of main(). The code I
posted wasn't my code, and I knew it had worked, so I assumed it was
correct, I didn't figure out that adding a main() was necessary given
there was a unit test.

And anyway, explaining in the book how to link is a necessary step imho.
This is why I made this page more visible in the Wiki:
http://prowiki.org/wiki4d/wiki.cgi?D__Tutorial/CompilingLinkingD
April 17, 2012
Re: Compiling Was: arrays and foreach
> In this case, I had to type:
> rdmd -unittest --main test.d
>
> Without the --main, I would get linker errors, and couldn't find the
> reason for these errors. Happily, someone here explained me that the
> effect of the --main flag was to insert a main() function just for this
> case.

That's not surprising, try to write a C program with no main:

─[ArchBox][/tmp]╼ touch foo.c
─[ArchBox][/tmp]╼ LANG=C gcc foo.c
/usr/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status

You get the linker errors, because there is no entry-point for your 
program, but that has nothing to do with D, it's a common behaviour for 
C, C++, D and other languages.


And the unittest(-switch) is also good relativly good explained at 
http://dlang.org/unittest.html (dlang -> search -> unittest)
April 17, 2012
Re: arrays and foreach
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
April 18, 2012
Re: arrays and foreach
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.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home