www.digitalmars.com         C & C++   DMDScript  

D - nesting comments?

reply Mark Shackelford <mark shackelford-family.org> writes:
Hi!

With neither nesting comments nor preprocessor #if 0 / #endif available,
how would a D programmer comment out a large section of code that
contains multiline /* */ comments? It won't work. This would force
everyone to use // comments everywhere just to be able at some future
point to comment out blocks of code.

Either make /* ... */ nest, or add another nesting comment type. You
know, the evil Modula (* ... *) kind. ;^)

Cheers!

--
Mark Shackelford
Aug 16 2001
parent reply "Walter" <walter digitalmars.com> writes:
To comment out a large block of code:

    if (0)
    {
        ... block of code ...
    }

The code must be syntactically correct, but not semantically
correct. -Walter

"Mark Shackelford" <mark shackelford-family.org> wrote in message
news:3B7C4DD2.8B5E9470 shackelford-family.org...
 Hi!

 With neither nesting comments nor preprocessor #if 0 / #endif available,
 how would a D programmer comment out a large section of code that
 contains multiline /* */ comments? It won't work. This would force
 everyone to use // comments everywhere just to be able at some future
 point to comment out blocks of code.

 Either make /* ... */ nest, or add another nesting comment type. You
 know, the evil Modula (* ... *) kind. ;^)

 Cheers!

 --
 Mark Shackelford

Aug 16 2001
next sibling parent reply Matt Busigin <mbusigin helios.spang.org.uk> writes:
Walter wrote:
 To comment out a large block of code:
 
     if (0)
     {
         ... block of code ...
     }
 
 The code must be syntactically correct, but not semantically
 correct. -Walter

This is not good enough - what about commenting out functions? Global declarations?
Aug 16 2001
parent reply "Walter" <walter digitalmars.com> writes:
"Matt Busigin" <mbusigin helios.spang.org.uk> wrote in message
news:3B7C4B0D.1040504 helios.spang.org.uk...
 Walter wrote:
 To comment out a large block of code:

     if (0)
     {
         ... block of code ...
     }

 The code must be syntactically correct, but not semantically
 correct. -Walter

This is not good enough - what about commenting out functions? Global declarations?

To comment out a function, you can wrap its entire code body with if(0){}. The function will still be there, but there'll be no code in it. Otherwise, you can use the debug attribute to get rid of blocks of declarations.
Aug 16 2001
next sibling parent reply "Angus Graham" <agraham_d agraham.ca> writes:
"Walter" <walter digitalmars.com> wrote in message news:9lhng3
 To comment out a function, you can wrap its entire code body with if(0){}.
 The function will still be there, but there'll be no code in it.

 you can use the debug attribute to get rid of blocks of declarations.

So to comment out a section of code I would have to change three lines in every function. That sucks! I don't understand - a decent editor colours comments so there's no question of which parts are commented out. I'm totally naive when it comes to compiler implementation, but I can't imagine how nested comments would complicate things on the implementation side. So why not allow them? Angus Graham
Aug 16 2001
next sibling parent reply "Kent Sandvik" <sandvik excitehome.net> writes:
"Angus Graham" <agraham_d agraham.ca> wrote in message
news:9lhphi$bts$1 digitaldaemon.com...

 I'm totally naive when it comes to compiler implementation, but I can't
 imagine how nested comments would complicate things on the implementation
 side.

I've always had problems with nested comments, the 'if 0' trick works just fine, lack of nested comments would not be a big minus for me when shopping for a good language. --Kent
Aug 16 2001
parent reply Erik Rounds <erikr aatrix.com> writes:
is there any reason why comments should not be able to be nested?  As long as
the number of /* match the number of */ I don't see any problem.  ie.

/*
    commented code 1
    /*
        commented code 2
    */
    commented code 3
*/

Why can't this be made to work?  Just increment a comment counter when a /* is
recieved and decrement it on a */.  Thanks.

Kent Sandvik wrote:

 "Angus Graham" <agraham_d agraham.ca> wrote in message
 news:9lhphi$bts$1 digitaldaemon.com...

 I'm totally naive when it comes to compiler implementation, but I can't
 imagine how nested comments would complicate things on the implementation
 side.

I've always had problems with nested comments, the 'if 0' trick works just fine, lack of nested comments would not be a big minus for me when shopping for a good language. --Kent

Aug 22 2001
parent reply "Walter" <walter digitalmars.com> writes:
Making it work is easy. The trouble is that C/C++ programmers are so used to
them not nesting, it can introduce bugs that are difficult to spot.

Erik Rounds wrote in message <3B83D77C.9ABB335C aatrix.com>...
is there any reason why comments should not be able to be nested?  As long

the number of /* match the number of */ I don't see any problem.  ie.

/*
    commented code 1
    /*
        commented code 2
    */
    commented code 3
*/

Why can't this be made to work?  Just increment a comment counter when a /*

recieved and decrement it on a */.  Thanks.

Kent Sandvik wrote:

 "Angus Graham" <agraham_d agraham.ca> wrote in message
 news:9lhphi$bts$1 digitaldaemon.com...

 I'm totally naive when it comes to compiler implementation, but I can't
 imagine how nested comments would complicate things on the



 side.

I've always had problems with nested comments, the 'if 0' trick works


 fine, lack of nested comments would not be a big minus for me when


 for a good language. --Kent


Aug 23 2001
next sibling parent "Rajiv Bhagwat" <dataflow vsnl.com> writes:
How about clear indication on the comment, based on the 'clipprep' style:

%a{  ,,............ %b{.............     %b} ................. %a}

Where the comments have 1 char identifier associated with them.

-- Rajiv


Walter <walter digitalmars.com> wrote in message
news:9m2bil$1unn$2 digitaldaemon.com...
 Making it work is easy. The trouble is that C/C++ programmers are so used

 them not nesting, it can introduce bugs that are difficult to spot.

 Erik Rounds wrote in message <3B83D77C.9ABB335C aatrix.com>...
is there any reason why comments should not be able to be nested?  As


 as
the number of /* match the number of */ I don't see any problem.  ie.

/*
    commented code 1
    /*
        commented code 2
    */
    commented code 3
*/

Why can't this be made to work?  Just increment a comment counter when a


 is
recieved and decrement it on a */.  Thanks.

Kent Sandvik wrote:

 "Angus Graham" <agraham_d agraham.ca> wrote in message
 news:9lhphi$bts$1 digitaldaemon.com...

 I'm totally naive when it comes to compiler implementation, but I




 imagine how nested comments would complicate things on the



 side.

I've always had problems with nested comments, the 'if 0' trick works


 fine, lack of nested comments would not be a big minus for me when


 for a good language. --Kent



Aug 23 2001
prev sibling parent reply Dan Hursh <hursh infonet.isl.net> writes:
	Is this true of most C/C++ programmers?  If I get confused about this
it is always because I assume they they do (or should) nest.  I have
never heard anyone complain about a compiler that did nest them.  I know
such compilers exist.  I just never thought this would be a huge problem
for folks to get used to.  I doubt it will be that big of a problem for
me to get used to it if they don't nest, but nesting seems more logical
to me, since you have unique open and close tokens for the comment.

Dan

Walter wrote:
 
 Making it work is easy. The trouble is that C/C++ programmers are so used to
 them not nesting, it can introduce bugs that are difficult to spot.
 
 Erik Rounds wrote in message <3B83D77C.9ABB335C aatrix.com>...
is there any reason why comments should not be able to be nested?  As long

the number of /* match the number of */ I don't see any problem.  ie.

/*
    commented code 1
    /*
        commented code 2
    */
    commented code 3
*/

Why can't this be made to work?  Just increment a comment counter when a /*

recieved and decrement it on a */.  Thanks.


Aug 23 2001
parent Russell Bornschlegel <kaleja estarcion.com> writes:
Dan Hursh wrote:
 
         Is this true of most C/C++ programmers?  If I get confused about this
 it is always because I assume they they do (or should) nest.  I have
 never heard anyone complain about a compiler that did nest them.  I know
 such compilers exist.  I just never thought this would be a huge problem
 for folks to get used to.  I doubt it will be that big of a problem for
 me to get used to it if they don't nest, but nesting seems more logical
 to me, since you have unique open and close tokens for the comment.

Back in the day, I believe Turbo C allowed a compiler switch to say whether comments nested. Nesting always seemed more logical and useful to me. Since switching to C++, though, I've never even been tempted to nest comments. -R
Aug 24 2001
prev sibling next sibling parent reply "Walter" <walter digitalmars.com> writes:
"Angus Graham" <agraham_d agraham.ca> wrote in message
news:9lhphi$bts$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message news:9lhng3
 To comment out a function, you can wrap its entire code body with


 The function will still be there, but there'll be no code in it.

 you can use the debug attribute to get rid of blocks of declarations.

So to comment out a section of code I would have to change three lines in every function. That sucks! I don't understand - a decent editor colours comments so there's no

 of which parts are commented out.

 I'm totally naive when it comes to compiler implementation, but I can't
 imagine how nested comments would complicate things on the implementation
 side.
 So why not allow them?
 Angus Graham

The problem with nested comments is that programmers don't expect them to be nested. BTW, there is nothing in D that prevents the C preprocessor from being run over it if you really miss it (or any other text macro processor)!
Aug 16 2001
parent "Sean L. Palmer" <spalmer iname.com> writes:
Programmers these days should be using a decent code editor, with syntax
highlighting, which would show any comment nesting bug right away.  Nobody
does "copy con: myprogram.d" anymore.  ;)  Use the tools available to you.

If the D spec says they nest, then D programmers will expect them to nest;
otherwise they won't.  What you decide will *be* what they expect.  So
decide wisely.  You've broken enough C compatibility already that tossing
out a tiny bit more won't kill anyone.  If you ask me, I say to hell with
C/C++ compatibility, make the language nice for its own sake.  Otherwise you
may as well expect D to do nothing more in the market than possibly
influence the next round of ANSI/ISO C++ standards committee because it
won't be enough of an improvement to C++ to warrant switching languages.

Sean

 The problem with nested comments is that programmers don't expect them to

 nested. BTW, there is nothing in D that prevents the C preprocessor from
 being run over it if you really miss it (or any other text macro

Oct 28 2001
prev sibling parent rogalsky <rogalsky theorie1.physik.uni-erlangen.de> writes:
Angus Graham wrote:

 I don't understand - a decent editor colours comments so there's no question
 of which parts are commented out.

-- \\|// (. .) +-----------------------------oOOo-(_)-oOOo----------------------------+ I Dipl. Phys. Olaf Rogalsky Institut f. Theo. Physik I I I Tel.: 09131 8528440 Univ. Erlangen-Nuernberg I I Fax.: 09131 8528444 Staudtstrasse 7 B3 I I rogalsky theorie1.physik.uni-erlangen.de D-91058 Erlangen I +----------------------------------------------------------------------+
Aug 17 2001
prev sibling parent "Sheldon Simms" <sheldon semanticedge.com> writes:
Im Artikel <9lhng3$a5d$1 digitaldaemon.com> schrieb "Walter"
<walter digitalmars.com>:

 "Matt Busigin" <mbusigin helios.spang.org.uk> wrote in message
 news:3B7C4B0D.1040504 helios.spang.org.uk...
 Walter wrote:
 To comment out a large block of code:

     if (0)
     {
         ... block of code ...
     }

 The code must be syntactically correct, but not semantically correct.
 -Walter

This is not good enough - what about commenting out functions? Global declarations?

To comment out a function, you can wrap its entire code body with if(0){}. The function will still be there, but there'll be no code in it. Otherwise, you can use the debug attribute to get rid of blocks of declarations.

maybe there should just be an ignore block... class ABC { int this () {...} ignore { int a1 () {...} int a2 () {...} } int b1 () {...} } -- Sheldon Simms / sheldon semanticedge.com
Aug 17 2001
prev sibling parent reply Christophe de Dinechin <descubes earthlink.net> writes:
Walter wrote:

 To comment out a large block of code:

     if (0)
     {
         ... block of code ...
     }

 The code must be syntactically correct, but not semantically
 correct. -Walter

As a compiler implementor, this makes me jump through the roof. In english: WHAAAT??? Let me combine that with other language features. Since I can reference forward declarations, I can write: void foo() { if (check_stuff) { ... } } void bar() { if (check_stuff) { ... } } const int check_stuff = 0; // make it 1 to enable stuff checking See where I'm going? Now you have to defer semantics of foo and bar until the first-level semantics of every possibly constant declaration in the file has been processes. That's going to be ugly. And last, what is considered a constant declaration? Let me build a corner case: const int check_stuff = (int) &bar - (int) &foo > 1024; Now, you have a recursive constant declaration, which impacts the size of the functions, and depends on it. Good luck processing that ;-) By the way, what if I want to disable a declaration? To remove debugging fields in a struct? Christophe
Aug 17 2001
parent "Walter" <walter digitalmars.com> writes:
Christophe de Dinechin wrote in message <3B7D3295.4886A523 earthlink.net>...
See where I'm going? Now you have to defer semantics of foo and bar until

first-level semantics of every possibly constant declaration in the file

been processes. That's going to be ugly.

No, it's not bad. The compiler makes multiple passes over the syntax tree to do the semantics. That's how forward references are resolved as well.
And last, what is considered a constant declaration? Let me build a corner
case:

    const int check_stuff = (int) &bar - (int) &foo > 1024;

Now, you have a recursive constant declaration, which impacts the size of

functions, and depends on it. Good luck processing that ;-)

A constant declaration is one that the compiler can figure out at compile time <g>.
By the way, what if I want to disable a declaration? To remove debugging
fields in a struct?

Use the debug attribute.
Aug 20 2001