[Haskell-cafe] A useful trick for inclusion and exclusion of commented code

Here's a little trick that I often use for inclusion and exclusion of a chunk of code. Imagine we have a function foo defined below. The three lines of code can be included or excluded just by a changing a single character --- that's the trick. So, the idea is to comment our code in a slightly different way like this to exclude the code from executing: 1 {- 2 myand :: Bool -> Bool -> Bool 3 myand a b = if a == b then True 4 else False 5 --} Notice how line 5 is commented '--}' instead of '-}'. Now, if we change the first character in line 11 from '{' to '-' the entire chunk of code from line 12-14 no longer remains comment. 11 -- 12 myand :: Bool -> Bool -> Bool 13 myand a b = if a == b then True 14 else False 15 --} This trick can also be applied to C++ comments. Best wishes, Sunil

Am Sonntag, 11. April 2004 23:14 schrieb Sunil KOTHARI:
Here's a little trick that I often use for inclusion and exclusion of a chunk of code. Imagine we have a function foo defined below. The three lines of code can be included or excluded just by a changing a single character --- that's the trick. So, the idea is to comment our code in a slightly different way like this to exclude the code from executing: 1 {- 2 myand :: Bool -> Bool -> Bool 3 myand a b = if a == b then True 4 else False 5 --}
Notice how line 5 is commented '--}' instead of '-}'. Now, if we change the first character in line 11 from '{' to '-' the entire chunk of code from line 12-14 no longer remains comment.
11 -- 12 myand :: Bool -> Bool -> Bool 13 myand a b = if a == b then True 14 else False 15 --}
This trick can also be applied to C++ comments.
Best wishes, Sunil
Hello, does this trick also work with GHC? I think that GHC needs a space after -- if these two dashes shall introduce an one line comment. Wolfgang

Wolfgang Jeltsch wrote:
does this trick also work with GHC? I think that GHC needs a space after -- if these two dashes shall introduce an one line comment.
It works with GHC, too, and this conforms to the H98 report, section 9.2 (Lexical Syntax): http://haskell.org/onlinereport/syntax-iso.html#sect9.2 The main reason for this to work is that the braces are classified as "special". If they were classified as "symbol", '--}' would be a "varsym", not the start of a comment. Cheers, S.
participants (3)
-
Sunil KOTHARI
-
Sven Panne
-
Wolfgang Jeltsch