Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #103345

Re: need {..} block, and switch case, need, and needs keywords

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: need {..} block, and switch case, need, and needs keywords
Date 2017-02-07 09:39 +0100
Organization A noiseless patient Spider
Message-ID <o7c11a$ti7$1@dont-email.me> (permalink)
References <cccf37f2-ec4b-4780-a406-ac52f46e8c79@googlegroups.com>

Show all headers | View raw


On 06/02/17 16:57, Rick C. Hodgin wrote:
> I had an idea for injecting code into a system which is tied to a needs
> keyword used later in flow control.  Consider the needs of this a switch
> {..} block, having some code which would only be executed if flow reached
> a certain point.
> 
>     switch (x)
>     {
>         case 0:
>             break;
>         case 1:
>             break;
>         case 2:
>             break;
> 
>         default:
>             value = get_something(); /* Possibly expensive call, therefore
>                                         only issued if flow reaches here */
>             // Continue testing
>             switch (x)
>             {
>                 case 3:
>                     // "value" is used here
>                     break;
>                 case 4:
>                     value2 = get_something_else();
>                     // "value" and "value2" are valid and in scope here
>                     break;
>                 default:
>                     // Not found
>                     break;
>             }
> 
>     }
> 

As is often the case with these ideas, I find it hard to understand why
you need to find new keywords and new syntax when there are already
reasonable ways to implement the effect - such as the code you posted above.

You have to think hard about how often a feature really would be useful.
 If it is rare, then it would be better for users to have to write
slightly odd code (such as above) than for them to have to learn new
syntaxes and new keywords for something they don't use often.

You also need to think about how changes like this would affect your
language.  A "switch" in C looks at the switch value, then jumps
directly to the corresponding case label - it does /not/ pass through
statements in between.  Be wary of how something like this "need" idea
might change existing concepts.


And rather than jumping into specialised cases, perhaps there is scope
for something that gives simpler and clearer code for the user, letting
the /compiler/ do the work in generate optimal code.  Consider a format:

	value = [[discardable]] get_something();

This tells the compiler that /if/ you use "value", then it needs to call
"get_something()".  But the call is "discardable" - if the result is not
used, the call can be discarded.  Then in each switch case, if "value"
is used the compiler must ensure that "get_something()" is called - if
it is not used, then it is (or may not be) not called.  This means that
the compiler does the work of figuring out the best arrangement, not the
user - the "needs" stuff is automatic.


(I am not saying that your "need" is necessarily a bad idea - I am
merely recommending you think about it more.)

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-06 07:57 -0800
  Re: need {..} block, and switch case, need, and needs keywords BartC <bc@freeuk.com> - 2017-02-06 16:23 +0000
    Re: need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-06 08:36 -0800
  Re: need {..} block, and switch case, need, and needs keywords David Brown <david.brown@hesbynett.no> - 2017-02-07 09:39 +0100
    Re: need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-07 07:32 -0800
      Re: need {..} block, and switch case, need, and needs keywords David Brown <david.brown@hesbynett.no> - 2017-02-08 09:43 +0100
        Re: need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-08 03:26 -0800
          Re: need {..} block, and switch case, need, and needs keywords David Brown <david.brown@hesbynett.no> - 2017-02-08 14:30 +0100
            Re: need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-08 05:38 -0800
              Re: need {..} block, and switch case, need, and needs keywords "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-02-08 05:43 -0800

csiph-web