digitalmars.D - Scope operator like in C++?
- TimK (20/20) Mar 23 2010 Hi!
- Steven Schveighoffer (56/80) Mar 23 2010 First, welcome!
- TimK (12/24) Mar 23 2010 Unfortunately I'm using Debian Stable 64 Bit right now and switching to ...
- Steven Schveighoffer (10/39) Mar 23 2010 I think the latest ldc is 64-bit. And there are a fair amount of people...
- Trass3r (2/6) Mar 23 2010 You forget the -H switch.
- Steven Schveighoffer (9/15) Mar 23 2010 No, I didn't. If you read my post, later I said that the compiler can d...
- Trass3r (1/8) Mar 23 2010 Ah indeed, missed that.
- bearophile (7/27) Mar 23 2010 Steven Schveighoffer has already told you most things. Generally when yo...
Hi!
I have a little (private) project in C++ and was thinking that I should give D
a chance (especially since it's not exactly new).
So I have a class TPerson where the declaration is in the header file and the
definition in the cpp-source file. Naturally a defined function looks
something like this:
void TCharacter::DoSomething(int x){...}
I do this for several reasons:
- The class would get really awkward to read through and understand, with all
the code in it - so I usually just put the declaration and the documentation
for public functions into it.
- a programmer who just wants to understand the class doesn't care about the
implementation.
Now I tried to make a class in D, too, following this style.
Unfortunately that doesn't work and I couldn't find a working alternative in
the documentation.
Is this not possible in D?
(I'm using GDC, so I guess it's still D 1.0)
Regards,
Tim
 Mar 23 2010
On Tue, 23 Mar 2010 06:41:36 -0400, TimK <qwesxrfvzgb web.de> wrote:
 Hi!
 I have a little (private) project in C++ and was thinking that I should  
 give D
 a chance (especially since it's not exactly new).
 So I have a class TPerson where the declaration is in the header file  
 and the
 definition in the cpp-source file. Naturally a defined function looks
 something like this:
 void TCharacter::DoSomething(int x){...}
 I do this for several reasons:
 - The class would get really awkward to read through and understand,  
 with all
 the code in it - so I usually just put the declaration and the  
 documentation
 for public functions into it.
 - a programmer who just wants to understand the class doesn't care about  
 the
 implementation.
 Now I tried to make a class in D, too, following this style.
 Unfortunately that doesn't work and I couldn't find a working  
 alternative in
 the documentation.
 Is this not possible in D?
 (I'm using GDC, so I guess it's still D 1.0)
First, welcome!
Second, GDC is horrifically old.  I'd recommend switching to ldc or the  
latest dmd 1.
Third, yes, it is somewhat possible.  But you have to think about it a  
different way.  When defining a class' implementation, you define  
everything inside the class.  This means that instead of this:
void TCharacter::DoSomething(int x){...}
int TCharacter::SomeOtherFunction() {...}
you do this:
class TCharacter
{
    void DoSomething(int x){...}
    int SomeOtherFunction(){...}
}
It actually turns out to be less code to write because you don't have to  
repeat the class name over and over, and it reads better IMO to someone  
trying to understand the implementation.
If you want to provide a "header file" for this, you may do so with a di  
file (d interface).  This file can have simply prototypes and  
documentation.  It will look exactly like your class definition, but does  
not need to contain your implementations.  In my example, the di file  
looks like this:
class TCharacter
{
    /**
     * Docs for DoSomething
     */
    void DoSomething(int x);
    /**
     * Docs for SomeOtherFunction()
     */
    int SomeOtherFunction();
}
The drawbacks of doing it this way are 1) you have to maintain two files  
instead of one, with almost identical contents and 2) di files without  
function implementations are not inlinable.
Note also, template functions must be included in the interface file,  
because the source must always be available to the compiler.
After explaining all that, and with my past experience with C++, I'd  
recommend *NOT* to do this.  The D compiler has a builtin documentation  
generator, so someone trying to use your functions will want to turn to  
that instead (much easier to read), and maintenance on two files that have  
to always be in sync is sucky to say the least.  The D compiler can  
auto-generate header files, but it is for the compiler's benefit, not a  
person's.  All docs are stripped out, and indentations are removed.  In  
addition, it tries to include implementations for functions which it  
thinks might be inlined.
For an example of what really good generated docs look like, see tango's  
docs: http://www.dsource.org/projects/tango/docs/current
My recommendation is to simply build your code inline, just like Java and  
take some getting used to, but I think you will appreciate the  
single-point-of-editing aspect of it.
Good luck!
-Steve
 Mar 23 2010
== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s articleSecond, GDC is horrifically old. I'd recommend switching to ldc or the latest dmd 1.Unfortunately I'm using Debian Stable 64 Bit right now and switching to either of those is out of the question (at least for now). Maybe I'll upgrade to Squeeze (and the LLVM compiler) soon. Anyway, thanks for the input, I'll keep that in mind if something won't work.The D compiler has a builtin documentation generator, so someone trying to use your functions will want to turn to that instead (much easier to read), and maintenance on two files that have to always be in sync is sucky to say the least. For an example of what really good generated docs look like, see tango's docs: http://www.dsource.org/projects/tango/docs/current My recommendation is to simply build your code inline, just like Java and take some getting used to, but I think you will appreciate the single-point-of-editing aspect of it.Oh, I didn't know that D had a builtin documentation generator... Well if the documentation will look like that then reading the class itself would become useless anyway. Guess I'll use that :) Thank you for your fast reply. Regards, Tim
 Mar 23 2010
On Tue, 23 Mar 2010 08:06:59 -0400, TimK <qwesxrfvzgb web.de> wrote:== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s articleI think the latest ldc is 64-bit. And there are a fair amount of people who use a 32-bit compiler on a 64-bit system, but I agree it's not savory.Second, GDC is horrifically old. I'd recommend switching to ldc or the latest dmd 1.Unfortunately I'm using Debian Stable 64 Bit right now and switching to either of those is out of the question (at least for now). Maybe I'll upgrade to Squeeze (and the LLVM compiler) soon. Anyway, thanks for the input, I'll keep that in mind if something won't work.Just a note, I think Tango uses dil (http://github.com/azizk/dil/ a D compiler written in D, not complete but the parser works) to generate the docs, so in order to make them look that good, you may have to do something similar. dmd also generates decent docs, I use it for dcollections along with candydoc. In general, I don't think there's any excuse to not use a doc generator :) -SteveThe D compiler has a builtin documentation generator, so someone trying to use your functions will want to turn to that instead (much easier to read), and maintenance on two files that have to always be in sync is sucky to say the least. For an example of what really good generated docs look like, see tango's docs: http://www.dsource.org/projects/tango/docs/current My recommendation is to simply build your code inline, just like Java and will take some getting used to, but I think you will appreciate the single-point-of-editing aspect of it.Oh, I didn't know that D had a builtin documentation generator... Well if the documentation will look like that then reading the class itself would become useless anyway. Guess I'll use that :)
 Mar 23 2010
If you want to provide a "header file" for this, you may do so with a di file (d interface). [...] The drawbacks of doing it this way are 1) you have to maintain two files instead of one, with almost identical contentsYou forget the -H switch. http://www.digitalmars.com/d/2.0/dmd-linux.html#interface_files
 Mar 23 2010
On Tue, 23 Mar 2010 10:08:08 -0400, Trass3r <un known.com> wrote:No, I didn't. If you read my post, later I said that the compiler can do it for you, but it is not suitable for human consumption. The header file generated has lots of formatting removed, and no comments. It doesn't even always strip out the implementation. The OP's intention was to maintain the header file with just the prototypes and docs so a user of his code would not have the implementations in the way of the documentation. -SteveIf you want to provide a "header file" for this, you may do so with a di file (d interface). [...] The drawbacks of doing it this way are 1) you have to maintain two files instead of one, with almost identical contentsYou forget the -H switch. http://www.digitalmars.com/d/2.0/dmd-linux.html#interface_files
 Mar 23 2010
No, I didn't. If you read my post, later I said that the compiler can do it for you, but it is not suitable for human consumption. The header file generated has lots of formatting removed, and no comments. It doesn't even always strip out the implementation. The OP's intention was to maintain the header file with just the prototypes and docs so a user of his code would not have the implementations in the way of the documentation.Ah indeed, missed that.
 Mar 23 2010
TimK:I have a little (private) project in C++ and was thinking that I should give D a chance (especially since it's not exactly new).Welcome, and I suggest you to use D V.1 language still, because it's simpler to learn and its compilers have less bugs. The latest LDC and DMD are OK, I prefer LDC on Ubuntu and DMD on Windows, plus something to automate building (because the D1 compiler is not able to find modules by itself as Java does). Eventually D2 will be debugged and refined enough to be used by novices too. Hopefully it will not take too much time. You can of course play with D2 too (DMD only), but you can find some bugs.So I have a class TPerson where the declaration is in the header file and the definition in the cpp-source file. Naturally a defined function looks something like this: void TCharacter::DoSomething(int x){...} I do this for several reasons: - The class would get really awkward to read through and understand, with all the code in it - so I usually just put the declaration and the documentation for public functions into it. - a programmer who just wants to understand the class doesn't care about the implementation. Now I tried to make a class in D, too, following this style. Unfortunately that doesn't work and I couldn't find a working alternative in the documentation. Is this not possible in D?Steven Schveighoffer has already told you most things. Generally when you change language it's better to use its specific idioms even if you don't like/understand them, because a well designed language is an organic thing, its parts are designed to be fit to each other. So what's bad and good is often language-specific. I've read people call D a Java++, or to define it a tidy C++, but in practice there are several differences between D and C++, for example the garbage collector changes many things. In D classes/structs have their methods written inside. The indentation helps the programmer see the grouping. In a module you can add related classes, if they aren't too much long. Bye, bearophile
 Mar 23 2010








 
  
  
 
 "Steven Schveighoffer" <schveiguy yahoo.com>
 "Steven Schveighoffer" <schveiguy yahoo.com> 