Thread overview
declaration with typedef
Dec 27, 2010
Hamad Mohammad
Dec 27, 2010
Andrej Mitrovic
Dec 27, 2010
Hamad Mohammad
Dec 27, 2010
David Nadlinger
Dec 27, 2010
Hamad Mohammad
Dec 28, 2010
Stanislav Blinov
Dec 28, 2010
Stanislav Blinov
Dec 28, 2010
Hamad Mohammad
Dec 28, 2010
David Nadlinger
Dec 28, 2010
Stanislav Blinov
December 27, 2010
I can't compile this code

void main()
{
    typedef int number;
    number x;
    x = 314;
    writeln(x);
}

December 27, 2010
That's because you're creating a new type with typedef, and writeln() doesn't know how to work with a "number" type. Use an alias:

void main()
{
   alias int number;
   number x;
   x = 314;
   writeln(x);
}

AFAIK typedef is scheduled for deprecation in D2 (if that's what you're using).

On 12/27/10, Hamad Mohammad <h.battel@hotmail.com> wrote:
> I can't compile this code
>
> void main()
> {
>     typedef int number;
>     number x;
>     x = 314;
>     writeln(x);
> }
>
>
December 27, 2010
excuse me

but why that code works

struct Human
{
    string name;
    int age;
}
void main()
{
    Human person1;
    person1.name = "aaaa";
    person1.age = 12;
    writeln(person1.age);
    typedef Human Mankind;
    Mankind person2;
    person2.name = "bbbb";
    person2.age = 14;
    writeln(person2.age);
}



December 27, 2010
On 12/27/10 3:44 PM, Hamad Mohammad wrote:
> excuse me
>
> but why that code works
>
>[…]

If you tried to assign person2 to person1 or vice versa, the same error would occur.

But as Andrei already said, typedef has been deemed deprecated in D2.

David
December 27, 2010
> If you tried to assign person2 to person1 or vice versa

how?

December 28, 2010
On 12/27/2010 06:41 PM, Hamad Mohammad wrote:
>> If you tried to assign person2 to person1 or vice versa
>
> how?
>

I don't think I got what David meant with it either. Assigning instances of the same type is perfectly valid as long as you do not define some very peculiar opAssign.

Andrej, on the other hand, made a perfect point.
A typedef is deprecated in D2. typedef in D2 differs from C/C++ one. What typedef does (in the old, D1 way) is introducing a new, distinct type. Many D constructs, especially templates, can handle only certain types. Those, depending on conditions, may or may not include user-defined types.

writeln requires that a given value is 'formattable' to string. It knows how to deal with numerics, strings, arrays, structs and classes. But it does not put out assumptions on unknown types (and typedef'd type is "unknown" to D's type system). There are some ways to "introduce" your types to certain constructs. For example, if you do
writeln(person1) in your code, you'll get "Human" on the console - this is a default way writeln handles structs. But if you define a method "toString" for your Human struct, e.g:

import std.conv;

struct Human
{
	//...
	string toString()
	{
		return text(name, ": ", age);
	}
}

, then writeln(person1) would output 'aaaa: 12' to the console.
(Mind that this 'toString' idiom may change, which is the effect of recent discussions about this certain topic).

Generally, if you want a "distinct" type in D2, define a struct (or a class if you want your type to have reference semantics). If you want a simple alias to existing type, use "alias" declaration (alias is an analog of C/C++ typedef, though the keyword itself does more than this. You can find out additional uses in documentaion or "The D Programming Language" book).

At this point, language gurus should start throwing rotten tomatoes at my general location, but I tried to explain the thing in the easiest way I could.
December 28, 2010
On 12/28/2010 04:09 AM, Stanislav Blinov wrote:

> typedef in D2 differs from C/C++ one.

Scratch that. I meant "in D" (having in mind D1 actually, oh well...)

December 28, 2010
== Quote from Stanislav Blinov (stanislav.blinov@gmail.com)'s article
> On 12/27/2010 06:41 PM, Hamad Mohammad wrote:
> >> If you tried to assign person2 to person1 or vice versa
> >
> > how?
> >
> I don't think I got what David meant with it either. Assigning instances
> of the same type is perfectly valid as long as you do not define some
> very peculiar opAssign.
> Andrej, on the other hand, made a perfect point.
> A typedef is deprecated in D2. typedef in D2 differs from C/C++ one.
> What typedef does (in the old, D1 way) is introducing a new, distinct
> type. Many D constructs, especially templates, can handle only certain
> types. Those, depending on conditions, may or may not include
> user-defined types.
> writeln requires that a given value is 'formattable' to string. It knows
> how to deal with numerics, strings, arrays, structs and classes. But it
> does not put out assumptions on unknown types (and typedef'd type is
> "unknown" to D's type system). There are some ways to "introduce" your
> types to certain constructs. For example, if you do
> writeln(person1) in your code, you'll get "Human" on the console - this
> is a default way writeln handles structs. But if you define a method
> "toString" for your Human struct, e.g:
> import std.conv;
> struct Human
> {
> 	//...
> 	string toString()
> 	{
> 		return text(name, ": ", age);
> 	}
> }
> , then writeln(person1) would output 'aaaa: 12' to the console.
> (Mind that this 'toString' idiom may change, which is the effect of
> recent discussions about this certain topic).
> Generally, if you want a "distinct" type in D2, define a struct (or a
> class if you want your type to have reference semantics). If you want a
> simple alias to existing type, use "alias" declaration (alias is an
> analog of C/C++ typedef, though the keyword itself does more than this.
> You can find out additional uses in documentaion or "The D Programming
> Language" book).
> At this point, language gurus should start throwing rotten tomatoes at
> my general location, but I tried to explain the thing in the easiest way
> I could.

thanks

December 28, 2010
On 12/28/10 2:09 AM, Stanislav Blinov wrote:
> I don't think I got what David meant with it either. Assigning instances
> of the same type is perfectly valid as long as you do not define some
> very peculiar opAssign.

The point here is that person1 and person2 are not instances of the same type, try compiling the following snippet:

---
struct Human {
   string name;
   int age;
}

void main() {
   Human person1;

   typedef Human Mankind;
   Mankind person2 = person1;
}
---

This might not be what Hamad wanted to know when he asked »but why that code works«, but I could think of no other reason why the snipped he posted should fail to compile.

David
December 28, 2010
28.12.2010 20:42, David Nadlinger пишет:
> On 12/28/10 2:09 AM, Stanislav Blinov wrote:
>> I don't think I got what David meant with it either. Assigning instances
>> of the same type is perfectly valid as long as you do not define some
>> very peculiar opAssign.
>
> The point here is that person1 and person2 are not instances of the same type, try compiling the following snippet:
>
> ---
> struct Human {
>    string name;
>    int age;
> }
>
> void main() {
>    Human person1;
>
>    typedef Human Mankind;
>    Mankind person2 = person1;
> }
> ---
>
Oh, right, I missed that one, sorry.

> This might not be what Hamad wanted to know when he asked »but why that code works«, but I could think of no other reason why the snipped he posted should fail to compile.
>
Yes, the original snippet should compile fine because typedef'd type isn't actually used in any way except for accessing fields, which of course have built-in types.