www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Where does this syntax comes from?

reply Florian <Florian_member pathlink.com> writes:
Maybe I just missed it reading the doc, or it is related to my last question
about the completeness of the doc, but here is something I can't figure out:

if you take object.d in phobos, there is a part like this

class Object
{
void print();
char[] toString();
uint toHash();
int opCmp(Object o);
int opEquals(Object o);
}

What this does is pretty obvious. It declares without defining the methods of
the class object. Where is it said that is is possible to delcare fonctions
without defining them?

the doc even says:
" C++ usually requires that functions and classes be declared twice - the
declaration that goes in the .h header file, and the definition that goes in the
c source file. This is an error prone and tedious process. Obviously, the
programmer should only need to write it once, and the compiler should then
extract the declaration information and make it available for symbolic
importing. This is exactly how D works."

Having read that, I did not even expect that it was possible to do it in D. But
it seems it is. Which brings me to the next part of my question: How (and where
in the case of object.d) is the definition writen?
Mar 01 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Florian wrote:

 What this does is pretty obvious. It declares without defining the methods of
 the class object. Where is it said that is is possible to delcare fonctions
 without defining them?

Welcome to the wonderful world of "import modules" in D ! Totally different from headers in C++... Ehrm. Not really. The import modules are like pointers or gotos.. Usually not needed, but there if you have to ?
 Having read that, I did not even expect that it was possible to do it in D. But
 it seems it is. Which brings me to the next part of my question: How (and where
 in the case of object.d) is the definition writen?

See the top of the file: "// Implementation is in internal\object.d" That file is private, and just ends up in the phobos object library. I posted a much longer explanation earlier, you can search for it. --anders
Mar 01 2005
parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Anders F Björklund" <afb algonet.se> wrote in message 
news:d0250s$2mb$1 digitaldaemon.com...
 Florian wrote:

 What this does is pretty obvious. It declares without defining the 
 methods of
 the class object. Where is it said that is is possible to delcare 
 fonctions
 without defining them?

Welcome to the wonderful world of "import modules" in D ! Totally different from headers in C++... Ehrm. Not really. The import modules are like pointers or gotos.. Usually not needed, but there if you have to ?

I don't understand your point. Are you being sarcastic? I can't tell. Separating interface from implementation has legitimate uses when you want to hide implementation information. It's an advanced maneuver but I wouldn't put it in the same bucket as pointers and gotos.
 Having read that, I did not even expect that it was possible to do it in 
 D. But
 it seems it is. Which brings me to the next part of my question: How (and 
 where
 in the case of object.d) is the definition writen?

See the top of the file: "// Implementation is in internal\object.d" That file is private, and just ends up in the phobos object library. I posted a much longer explanation earlier, you can search for it. --anders

I just want to add the key step if you want to try this on your own code is that you should not to link both the interface and the implementation together since that will cause duplicate definition errors. -Ben
Mar 01 2005
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Ben Hinkle wrote:

Welcome to the wonderful world of "import modules" in D !
Totally different from headers in C++... Ehrm. Not really.

The import modules are like pointers or gotos..
Usually not needed, but there if you have to ?

I don't understand your point. Are you being sarcastic? I can't tell.

Sorry, I was out of smilies for the day it seems like... :-)
 Separating interface from implementation has legitimate uses when you want 
 to hide implementation information. It's an advanced maneuver but I wouldn't 
 put it in the same bucket as pointers and gotos.

I was kinda sarcastic, just because the docs cheerfully says that it doesn't ever need forward declarations and then Phobos uses them...
I posted a much longer explanation earlier, you can search for it.

I just want to add the key step if you want to try this on your own code is that you should not to link both the interface and the implementation together since that will cause duplicate definition errors.

Same as with "extern" modules that are used to import C variables... Here is my earlier post on the subject: digitalmars.D/15626 It discusses import modules, implementation modules, data modules --anders
Mar 01 2005