D newb

Steven Schveighoffer schveiguy at yahoo.com
Sun Aug 17 19:47:57 PDT 2008


"Tim H" wrote
> bearophile Wrote:
>
>
>> >         public int m_int = 0;
>> This is enough:
>>         public int m_int;
>> Because variables are initialized to .init by default (and there's a way 
>> to avoid that), and int.init is 0.
>
> So every variable is 0 initialized?

No, every variable is initialized to a default.  Most of the time, this is 
zero (or null).  However, sometimes, it is not.  For instance, floating 
point numbers are initialized to NaN.

If you want to know the initial value, it is stored as the inherent 'init' 
property.  So for example, all ints are initialized to int.init (unless you 
override that with an assignment).

>> > int main(string[] args)
>>
>> You can use this, because the return value is automatic:
>> void main()
>
> If I want to return a value other than 0, how do I do it?

The way you originally had it.  I think bearophile was saying that in your 
particular example, you don't need to use it, because you never return 
anything but zero (EXIT_SUCCESS).

>
>> To print writef/writefln is generally better, because typesafe (but it 
>> makes your executable fatter):
>> writefln(foo %d", f.m_int);
>
> What does that mean - to be typesafe?  Can you give me a real example of 
> where writef() is better than printf().

Sure, if you do:

printf("foo %d\n", "hello"), you will get the pointer of hello as an 
integer.

if you do

writef("foo, %d\n", "hello"), you get a type exception (I think), because 
writef is passed the type of all  its arguments.

A better example:

printf("foo %s\n", 0), which gives you a segfault.

With writef, %s is a 'catch all'.  It means, 'check the type of the argument 
to see how to print it', and in the case:

writef("foo %s\n", 0), you will get a 0.

Most of the other % types are for formatting, like if you wanted a certain 
precision floating point, or to 0-pad, etc.  Most of the time, you can just 
use %s:

writef("%s, %s is an int, %s is a float, %s is a long", "hi", 5, 4.3, 
1000000000000);

>
> I'm looking for a good D tutorial.  Something that would have all these 
> answers.  Can you recommend one?  I've been writing C for a dozen years 
> and C++ for a few, as well as other stuff here and there.

As I only use Tango (http://www.dsource.org/projects/tango), I don't know of 
tutorials that uses Phobos, but I would imagine there should be something on 
the digitalmars site.  There's plenty of tutorials and instruction (and a 
book in print) if you want to use Tango.

-Steve 





More information about the Digitalmars-d mailing list