www.digitalmars.com         C & C++   DMDScript  

D - static exceptions?

reply Hauke Duden <H.NS.Duden gmx.net> writes:
While writing another post on the topic of warnings in switch statements 
I got an interesting idea. I'm not sure whether it is really new, so 
please bear with me if it has already been implemented in some language 
from 1965 or something ;).

One of the main problems with compiler warnings is that sometimes they 
can hint at a coding error, but at other times they get issued for 
perfectly legal code that you know is right.

If you're like me and lots of warnings in your code irritate you (they 
make it harder to find the real errors), then at one time or another you 
probably have changed parts of your code to prevent the warning from 
occuring. Or maybe switched off the warning completely using some pragma.

The problem with changing your code is that you only have to do it 
because the compiler is "a whiny bitch". You cannot write code in what 
may be the best way in a particular situation. This is really only a 
minor issue because circumventing warnings is usually pretty easy, but 
it is still a valid gripe.

And disabling warnings with a pragma is a hack. It can also be dangerous 
because many people tend to do it globally for a file (or even the whole 
project). So maybe they won't get the warning in another part of the 
code where it actually IS a mistake.

Realizing that I thought about the recent tendency in new languages to 
mimick runtime behaviour for compile time issues. D's static asserts are 
just one example of that.

So why not have static exceptions? I.e. the ability to "catch" warnings 
that occur at compile time and prevent them from being printed? The 
syntax would be a lot cleaner than littering the code with pragma's and 
at the same time it would be explicitly obvious that you know about the 
warning and it can be safely ignored.

Here's an example of what it might look like:

static try
{
     int foo(int x)
     {
         while (x)
         {
             blahblah
             if (blah blah)
                 return 3;
         }
     }
}
static catch(NoReturnWarning)
{
	//ignore
}


or maybe the try could be implicit...


int foo(int x)
{
     while (x)
     {
         blahblah
         if (blah blah)
             return 3;
     }
}
static catch(NoReturnWarning)
{
	//ignore
}



I'm still not sure whether something like this is actually a good thing. 
  I just thought I'd write it down before it slips my mind again ;).


Hauke
Nov 27 2003
next sibling parent reply davepermen <davepermen_member pathlink.com> writes:
i always threat all warnings as errors and correct them in a way both me and the
compiler are happy. a warning means he is not sure about something is fine.
explain him in a nice way that it is: by solving his issue.

thats best imho. proper coding is a good thing:D

In article <bq624f$2iej$1 digitaldaemon.com>, Hauke Duden says...
While writing another post on the topic of warnings in switch statements 
I got an interesting idea. I'm not sure whether it is really new, so 
please bear with me if it has already been implemented in some language 
from 1965 or something ;).

One of the main problems with compiler warnings is that sometimes they 
can hint at a coding error, but at other times they get issued for 
perfectly legal code that you know is right.

If you're like me and lots of warnings in your code irritate you (they 
make it harder to find the real errors), then at one time or another you 
probably have changed parts of your code to prevent the warning from 
occuring. Or maybe switched off the warning completely using some pragma.

The problem with changing your code is that you only have to do it 
because the compiler is "a whiny bitch". You cannot write code in what 
may be the best way in a particular situation. This is really only a 
minor issue because circumventing warnings is usually pretty easy, but 
it is still a valid gripe.

And disabling warnings with a pragma is a hack. It can also be dangerous 
because many people tend to do it globally for a file (or even the whole 
project). So maybe they won't get the warning in another part of the 
code where it actually IS a mistake.

Realizing that I thought about the recent tendency in new languages to 
mimick runtime behaviour for compile time issues. D's static asserts are 
just one example of that.

So why not have static exceptions? I.e. the ability to "catch" warnings 
that occur at compile time and prevent them from being printed? The 
syntax would be a lot cleaner than littering the code with pragma's and 
at the same time it would be explicitly obvious that you know about the 
warning and it can be safely ignored.

Here's an example of what it might look like:

static try
{
     int foo(int x)
     {
         while (x)
         {
             blahblah
             if (blah blah)
                 return 3;
         }
     }
}
static catch(NoReturnWarning)
{
	//ignore
}


or maybe the try could be implicit...


int foo(int x)
{
     while (x)
     {
         blahblah
         if (blah blah)
             return 3;
     }
}
static catch(NoReturnWarning)
{
	//ignore
}



I'm still not sure whether something like this is actually a good thing. 
  I just thought I'd write it down before it slips my mind again ;).


Hauke

Nov 28 2003
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
davepermen wrote:

i always threat all warnings as errors and correct them in a way both me and the
compiler are happy. a warning means he is not sure about something is fine.
explain him in a nice way that it is: by solving his issue.

thats best imho. proper coding is a good thing:D
  

Me to. In C++, I turn warning up to max level and put the switch "treat all warnings as errors" on. It's has saved me many problems later down the track. I've very rarely (1/1000000) found a warning/error that I couldn't fix (without using an ignore pragma). D should have need for this pragma hack (or anything similar), although I do wish that it would take on more of C++'s warnings, as error messages. -Anderson
Nov 28 2003
next sibling parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
I havent used .76, but up to now there were no warnings, only errors is
still the case ?

C

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:bq7tkr$2ba5$1 digitaldaemon.com...
 davepermen wrote:

i always threat all warnings as errors and correct them in a way both me


compiler are happy. a warning means he is not sure about something is


explain him in a nice way that it is: by solving his issue.

thats best imho. proper coding is a good thing:D

Me to. In C++, I turn warning up to max level and put the switch "treat all warnings as errors" on. It's has saved me many problems later down the track. I've very rarely (1/1000000) found a warning/error that I couldn't fix (without using an ignore pragma). D should have need for this pragma hack (or anything similar), although I do wish that it would take on more of C++'s warnings, as error messages. -Anderson

Nov 28 2003
next sibling parent reply davepermen <davepermen_member pathlink.com> writes:
no clue. haven't had any warnings myself in D as well, including 0.76..
and i think thats a good thing. eighter it should be correct code, or not. but
not "uhm.. could be good, could be bad, you know?" fuzzy logic code..

what could be cool would be sort of an "implicit created data" report.

like where it created implicit conversions, like..
[printout]
the statement float x = width/height*2 + foo + bar;
got interpreted as
float = to_float( int idiv int imul 2 ) fadd foo fadd bar;
[/printout]

or
[printout]
class Obj got an implicit constructor, and destructor.
[/printout]

just some.. information on what the compiler did by itself.

where needed, that is.. (would be great for an ide to just select a line, and
get some "informations" about what the compiler thinks about it).

no warnings. errors only. code is good, or bad.


In article <bq8010$2et6$1 digitaldaemon.com>, Charles Sanders says...
I havent used .76, but up to now there were no warnings, only errors is
still the case ?

C

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:bq7tkr$2ba5$1 digitaldaemon.com...
 davepermen wrote:

i always threat all warnings as errors and correct them in a way both me


compiler are happy. a warning means he is not sure about something is


explain him in a nice way that it is: by solving his issue.

thats best imho. proper coding is a good thing:D

Me to. In C++, I turn warning up to max level and put the switch "treat all warnings as errors" on. It's has saved me many problems later down the track. I've very rarely (1/1000000) found a warning/error that I couldn't fix (without using an ignore pragma). D should have need for this pragma hack (or anything similar), although I do wish that it would take on more of C++'s warnings, as error messages. -Anderson


Nov 28 2003
next sibling parent "Charles Sanders" <sanders-consulting comcast.net> writes:
Yea i agree I like the errors only, that would be cool for the compiler,
although Im not sure where or if it makes any of these conversions, its
really strongly typed which I love.

C


"davepermen" <davepermen_member pathlink.com> wrote in message
news:bq809l$2fc8$1 digitaldaemon.com...
 no clue. haven't had any warnings myself in D as well, including 0.76..
 and i think thats a good thing. eighter it should be correct code, or not.

 not "uhm.. could be good, could be bad, you know?" fuzzy logic code..

 what could be cool would be sort of an "implicit created data" report.

 like where it created implicit conversions, like..
 [printout]
 the statement float x = width/height*2 + foo + bar;
 got interpreted as
 float = to_float( int idiv int imul 2 ) fadd foo fadd bar;
 [/printout]

 or
 [printout]
 class Obj got an implicit constructor, and destructor.
 [/printout]

 just some.. information on what the compiler did by itself.

 where needed, that is.. (would be great for an ide to just select a line,

 get some "informations" about what the compiler thinks about it).

 no warnings. errors only. code is good, or bad.


 In article <bq8010$2et6$1 digitaldaemon.com>, Charles Sanders says...
I havent used .76, but up to now there were no warnings, only errors is
still the case ?

C

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:bq7tkr$2ba5$1 digitaldaemon.com...
 davepermen wrote:

i always threat all warnings as errors and correct them in a way both




and the
compiler are happy. a warning means he is not sure about something is


explain him in a nice way that it is: by solving his issue.

thats best imho. proper coding is a good thing:D

Me to. In C++, I turn warning up to max level and put the switch



 all warnings as errors" on. It's has saved me many problems later down
 the track.  I've very rarely (1/1000000) found a warning/error that I
 couldn't fix (without using an ignore pragma).  D should have need for
 this pragma hack (or anything similar), although I do wish that it



 take on more of C++'s warnings, as error messages.

 -Anderson



Nov 28 2003
prev sibling parent "Charles Sanders" <sanders-consulting comcast.net> writes:
Oh i see , yea that would be kickass.

C

"davepermen" <davepermen_member pathlink.com> wrote in message
news:bq809l$2fc8$1 digitaldaemon.com...
 no clue. haven't had any warnings myself in D as well, including 0.76..
 and i think thats a good thing. eighter it should be correct code, or not.

 not "uhm.. could be good, could be bad, you know?" fuzzy logic code..

 what could be cool would be sort of an "implicit created data" report.

 like where it created implicit conversions, like..
 [printout]
 the statement float x = width/height*2 + foo + bar;
 got interpreted as
 float = to_float( int idiv int imul 2 ) fadd foo fadd bar;
 [/printout]

 or
 [printout]
 class Obj got an implicit constructor, and destructor.
 [/printout]

 just some.. information on what the compiler did by itself.

 where needed, that is.. (would be great for an ide to just select a line,

 get some "informations" about what the compiler thinks about it).

 no warnings. errors only. code is good, or bad.


 In article <bq8010$2et6$1 digitaldaemon.com>, Charles Sanders says...
I havent used .76, but up to now there were no warnings, only errors is
still the case ?

C

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:bq7tkr$2ba5$1 digitaldaemon.com...
 davepermen wrote:

i always threat all warnings as errors and correct them in a way both




and the
compiler are happy. a warning means he is not sure about something is


explain him in a nice way that it is: by solving his issue.

thats best imho. proper coding is a good thing:D

Me to. In C++, I turn warning up to max level and put the switch



 all warnings as errors" on. It's has saved me many problems later down
 the track.  I've very rarely (1/1000000) found a warning/error that I
 couldn't fix (without using an ignore pragma).  D should have need for
 this pragma hack (or anything similar), although I do wish that it



 take on more of C++'s warnings, as error messages.

 -Anderson



Nov 28 2003
prev sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Charles Sanders wrote:

I havent used .76, but up to now there were no warnings, only errors is
still the case ?

C

  

are errors in C++, anyways). I think D should adopt more (not all) of C++'s warnings as errors (not as warnings -> which D doesn't have and Walter strongly disagrees with). -Anderson
Nov 28 2003
prev sibling parent reply "Matthew Wilson" <matthew.hat stlsoft.dot.org> writes:
i always threat all warnings as errors and correct them in a way both me


compiler are happy. a warning means he is not sure about something is


explain him in a nice way that it is: by solving his issue.

thats best imho. proper coding is a good thing:D

Me to. In C++, I turn warning up to max level and put the switch "treat all warnings as errors" on. It's has saved me many problems later down the track. I've very rarely (1/1000000) found a warning/error that I couldn't fix (without using an ignore pragma).

Indeed. The only one I can think of is GCC 2.X's stupid assertion that having const and non-const versions of the same operator was unwise. Thankfully 3.X does not do that. Although, you must have written a *lot* of code to come to that statistic ;)
Nov 28 2003
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Matthew Wilson wrote:

Me to.  In C++, I turn warning up to max level and put the switch "treat
all warnings as errors" on. It's has saved me many problems later down
the track.  I've very rarely (1/1000000) found a warning/error that I
couldn't fix (without using an ignore pragma).
    

Indeed. The only one I can think of is GCC 2.X's stupid assertion that having const and non-const versions of the same operator was unwise. Thankfully 3.X does not do that. Although, you must have written a *lot* of code to come to that statistic ;)

exaggeration. Iíve probably seen 2 (which I canít remember of hand). Although I have written a lot of code -> how much is considered a lot? Probably about 1million lines, considering that my last project was just under 100,000. I bet most of the professional programmers in this group have written much more then me. Walter would probably be over the hundreds of millions and I hope thatís not an insult :(. But whose counting ;) One persons line is another personís china wall. -Anderson
Nov 28 2003
prev sibling parent reply Hauke Duden <H.NS.Duden gmx.net> writes:
davepermen wrote:

 i always threat all warnings as errors and correct them in a way both me and
the
 compiler are happy. a warning means he is not sure about something is fine.
 explain him in a nice way that it is: by solving his issue.

So do I. I hate having warnings in my code, as I have stated in my post. I didn't want to imply that catching/disabling warnings regularly is a good thing. I merely found the concept of static exceptions interesting. And I still do :). If you think this through to the end you end up replacing simple templates with generic compile-time functions that generate classes. How cool is that?! Is there any language that does this kind of thing? I know something similar could be done with Lua, since in Lua a "class" is really a runtime construct. But are there any compiled languages that have something like compile time class generating functions? I'd be interested in learning about such languages, if they exist... Hauke
Nov 28 2003
next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Hauke Duden wrote:

 davepermen wrote:

 i always threat all warnings as errors and correct them in a way both 
 me and the
 compiler are happy. a warning means he is not sure about something is 
 fine.
 explain him in a nice way that it is: by solving his issue.

So do I. I hate having warnings in my code, as I have stated in my post. I didn't want to imply that catching/disabling warnings regularly is a good thing. I merely found the concept of static exceptions interesting. And I still do :). If you think this through to the end you end up replacing simple templates with generic compile-time functions that generate classes. How cool is that?! Is there any language that does this kind of thing? I know something similar could be done with Lua, since in Lua a "class" is really a runtime construct. But are there any compiled languages that have something like compile time class generating functions? I'd be interested in learning about such languages, if they exist... Hauke

Parhaps if instead of warnings, if user-defined-error (exceptions) where caught, then static exceptions could have some real benefits. Ie instead of warnings catch compile time errors. I say, a bug caught at compile time saves nine. Although, I haven't really thought about how it would look (or even if it's feasible -> I'll leave that to others). It also leads into that "compile time function" thing I suggested ;) which no-body else seemed to like :( -Anderson
Nov 28 2003
prev sibling next sibling parent Antti =?iso-8859-1?Q?Syk=E4ri?= <jsykari gamma.hut.fi> writes:
In article <bq8ml5$df9$1 digitaldaemon.com>, Hauke Duden wrote:
 I didn't want to imply that catching/disabling warnings regularly is a 
 good thing. I merely found the concept of static exceptions interesting.
 
 And I still do :). If you think this through to the end you end up 
 replacing simple templates with generic compile-time functions that 
 generate classes. How cool is that?!
 
 Is there any language that does this kind of thing? I know something 
 similar could be done with Lua, since in Lua a "class" is really a 
 runtime construct. But are there any compiled languages that have 
 something like compile time class generating functions? I'd be 
 interested in learning about such languages, if they exist...

Me too. There are plans underway for making compile time functions in C++ -- http://www.vandevoorde.com/Daveed/News/Archives/000014.html -- and Haskell -- http://research.microsoft.com/Users/simonpj/papers/meta-haskell/ -- but these projects still seem very experimental. -Antti
Dec 01 2003
prev sibling next sibling parent reply Elias Martenson <elias-m algonet.se> writes:
Hauke Duden wrote:

 Is there any language that does this kind of thing? I know something 
 similar could be done with Lua, since in Lua a "class" is really a 
 runtime construct. But are there any compiled languages that have 
 something like compile time class generating functions? I'd be 
 interested in learning about such languages, if they exist...

Yes, Common Lisp has all this. Like I mentioned in the previous pos, there is a construct called eval-when, which allows you to run certain code when the file is compiled, loaded into the VM, or when it's evaluated. The example in the documentation (http://www-2.cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/Body/s eope_eval-when.html ) uses this feature to alter the reader (parser) in order to extend the language before the rest of the code is compiled. Common Lisp also has a completely programmable metamodel for the object system. Elias
Dec 01 2003
parent Hauke Duden <H.NS.Duden gmx.net> writes:
Elias Martenson wrote:
 Hauke Duden wrote:
 
 Is there any language that does this kind of thing? I know something 
 similar could be done with Lua, since in Lua a "class" is really a 
 runtime construct. But are there any compiled languages that have 
 something like compile time class generating functions? I'd be 
 interested in learning about such languages, if they exist...

Yes, Common Lisp has all this. Like I mentioned in the previous pos, there is a construct called eval-when, which allows you to run certain code when the file is compiled, loaded into the VM, or when it's evaluated. The example in the documentation (http://www-2.cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/Body/s eope_eval-when.html ) uses this feature to alter the reader (parser) in order to extend the language before the rest of the code is compiled. Common Lisp also has a completely programmable metamodel for the object system.

Thanks! I'll take a look at it! Hauke
Dec 01 2003
prev sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
Hauke Duden wrote:

 Is there any language that does this kind of thing? I know something 
 similar could be done with Lua, since in Lua a "class" is really a 
 runtime construct. But are there any compiled languages that have 
 something like compile time class generating functions? I'd be 
 interested in learning about such languages, if they exist...

See compile-time reflection. http://www.csg.is.titech.ac.jp/~chiba/openc++.html http://www.csg.is.titech.ac.jp/openjava/ -eye
Dec 03 2003
prev sibling parent Elias Martenson <elias-m algonet.se> writes:
Hauke Duden wrote:

 So why not have static exceptions? I.e. the ability to "catch" warnings 
 that occur at compile time and prevent them from being printed? The 
 syntax would be a lot cleaner than littering the code with pragma's and 
 at the same time it would be explicitly obvious that you know about the 
 warning and it can be safely ignored.

I think what you really want here is compile-time code execution. Somehting similar to Common Lisp's eval-when. (more information at http://www-2.cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/Body/s eope_eval-when.html ) However, D is a statically compiled langiage and doesn't support compiler-time execution. Therefore youre static (but conditional) code would have to be executed at run time. Not a good tradeoff compared to actually finixing the bug that cuased the warning in the first place. Elias
Dec 01 2003