Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 14, 2003 Labeled statement and empty statement | ||||
---|---|---|---|---|
| ||||
Hi, In C it is common to see functions like this: void foo(void) { // do something useful if (error) { goto done; } // do something useful done: // clean up } In are more concrete example, it could end like this: ... done: fclose(fp); free(ptr); } The code is maintained, and for some reason there is no need for fclose() and free() anymore. But there are numerous goto's to the label, and we want to keep the single exit point. So it is changed to: ... done: } But this will not compile, because there is no statement after "done:". In C we need to use the empty statement like this: ... done: ; } Silly, isn't it? In D, if the documentation is correct, it is even worse, because there is no empty statement. So we need to write something like: ... done: { } } or ... done: 0; } But it appears that DMD actually implements the empty statement, so the documentation and DMD is out of sync here. I just wish there was no need for the empty statement. It t would be more elegant if labels didn't need to prefix a statement. Regards, Martin |
August 14, 2003 Re: Labeled statement and empty statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin M. Pedersen | "Martin M. Pedersen" <martin@moeller-pedersen.dk> wrote in message news:bhgbl8$ac3$1@digitaldaemon.com... > Hi, > > In C it is common to see functions like this: > > void foo(void) > { > // do something useful > if (error) { > goto done; > } > // do something useful > done: > // clean up > } > > In are more concrete example, it could end like this: > > ... > done: > fclose(fp); > free(ptr); > } > > The code is maintained, and for some reason there is no need for fclose() > and free() anymore. But there are numerous goto's to the label, and we want > to keep the single exit point. So it is changed to: > > ... > done: > } > > But this will not compile, because there is no statement after "done:". In C > we need to use the empty statement like this: > > ... > done: > ; > } > > Silly, isn't it? In D, if the documentation is correct, it is even worse, because there is no empty statement. So we need to write something like: > > ... > done: > { } > } > > or > ... > done: > 0; > } > > But it appears that DMD actually implements the empty statement, so the > documentation and DMD is out of sync here. > I just wish there was no need for the empty statement. It t would be more > elegant if labels didn't need to prefix a statement. > you should use a labled block and change your goto's to a break done : { .... if ( error ) { break done; } ... return; } // clean up. or if you want to be more OO change it to try { if ( error ) { throw new Exception(); } // or a staticly held exception if you worred about performance } catch( Exception e ) { } // [finally ....] |
August 14, 2003 Re: Labeled statement and empty statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin M. Pedersen | > It t would be more > elegant if labels didn't need to prefix a statement. It would be more elegenat if you didnt use goto's :P. Charles "Martin M. Pedersen" <martin@moeller-pedersen.dk> wrote in message news:bhgbl8$ac3$1@digitaldaemon.com... > Hi, > > In C it is common to see functions like this: > > void foo(void) > { > // do something useful > if (error) { > goto done; > } > // do something useful > done: > // clean up > } > > In are more concrete example, it could end like this: > > ... > done: > fclose(fp); > free(ptr); > } > > The code is maintained, and for some reason there is no need for fclose() > and free() anymore. But there are numerous goto's to the label, and we want > to keep the single exit point. So it is changed to: > > ... > done: > } > > But this will not compile, because there is no statement after "done:". In C > we need to use the empty statement like this: > > ... > done: > ; > } > > Silly, isn't it? In D, if the documentation is correct, it is even worse, because there is no empty statement. So we need to write something like: > > ... > done: > { } > } > > or > ... > done: > 0; > } > > But it appears that DMD actually implements the empty statement, so the > documentation and DMD is out of sync here. > I just wish there was no need for the empty statement. It t would be more > elegant if labels didn't need to prefix a statement. > > > Regards, > Martin > > |
August 14, 2003 Re: Labeled statement and empty statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Sanders | "Charles Sanders" <sanders-consulting@comcast.net> wrote in message news:bhgdqv$cfh$1@digitaldaemon.com... > It would be more elegenat if you didnt use goto's :P. I know, but still, "goto" is part of the language. Regards, Martin |
August 15, 2003 Re: Labeled statement and empty statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin M. Pedersen | On Thu, 14 Aug 2003 19:34:43 +0200 (08/15/03 03:34:43) , Martin M. Pedersen <martin@moeller-pedersen.dk> wrote: > "Charles Sanders" <sanders-consulting@comcast.net> wrote in message > news:bhgdqv$cfh$1@digitaldaemon.com... >> It would be more elegenat if you didnt use goto's :P. > > I know, but still, "goto" is part of the language. > Yes, but that neither compels you to use it, or to make it a part of your 'language'. -- Derek |
August 15, 2003 Re: Labeled statement and empty statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | I suppose the goto statement isn't a big issue, but it's the
first thing I would want to remove from all languages except
it's equivalent, the jump statement on assembler, where it's
needed. Otherwise nothing worse or uglier than a goto statement.
Derek Parnell wrote:
> On Thu, 14 Aug 2003 19:34:43 +0200 (08/15/03 03:34:43)
> , Martin M. Pedersen <martin@moeller-pedersen.dk> wrote:
>
>> "Charles Sanders" <sanders-consulting@comcast.net> wrote in message
>> news:bhgdqv$cfh$1@digitaldaemon.com...
>>
>>> It would be more elegenat if you didnt use goto's :P.
>>
>>
>> I know, but still, "goto" is part of the language.
>>
>
> Yes, but that neither compels you to use it, or to make it a part of your 'language'.
>
>
>
|
August 15, 2003 Re: Labeled statement and empty statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frank D. Wills | I like goto. "Frank D. Wills" <fdwills@sandarh.com> wrote in message news:bhh9ic$1853$1@digitaldaemon.com... > I suppose the goto statement isn't a big issue, but it's the first thing I would want to remove from all languages except it's equivalent, the jump statement on assembler, where it's needed. Otherwise nothing worse or uglier than a goto statement. > > > Derek Parnell wrote: > > On Thu, 14 Aug 2003 19:34:43 +0200 (08/15/03 03:34:43) > > , Martin M. Pedersen <martin@moeller-pedersen.dk> wrote: > > > >> "Charles Sanders" <sanders-consulting@comcast.net> wrote in message news:bhgdqv$cfh$1@digitaldaemon.com... > >> > >>> It would be more elegenat if you didnt use goto's :P. > >> > >> > >> I know, but still, "goto" is part of the language. > >> > > > > Yes, but that neither compels you to use it, or to make it a part of your 'language'. > > > > > > > |
August 15, 2003 Re: Labeled statement and empty statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | On Fri, 15 Aug 2003 00:49:10 -0400 (08/15/03 14:49:10) , Vathix <vathix@dprogramming.com> wrote: > I like goto. And I like McDonald's BigMacs, but it doesn't mean they're good for me. -- Derek |
August 15, 2003 Re: Labeled statement and empty statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | > > I know, but still, "goto" is part of the language.
> >
>
> Yes, but that neither compels you to use it, or to make it a part of your 'language'.
If it's in, then its use should make sense.
(Not that I'm coming down on any side of the goto debate here.)
|
August 15, 2003 Re: Labeled statement and empty statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frank D. Wills | "Frank D. Wills" <fdwills@sandarh.com> wrote in message news:bhh9ic$1853$1@digitaldaemon.com... > I suppose the goto statement isn't a big issue, but it's the first thing I would want to remove from all languages except it's equivalent, the jump statement on assembler, where it's needed. Otherwise nothing worse or uglier than a goto statement. Don't mean to offend, but this sounds terribly naive. Just because something is rightly deprecated in most circumstances does not mean it does not have a use. I rarely use it, probably a couple of times a year, but when I do it is the appropriate tool for the job in hand, so I use it. |
Copyright © 1999-2021 by the D Language Foundation