Re: Help with structs



On 2010-08-27, Jack Trades <jacktradespublic@xxxxxxxxx> wrote:
Maybe I'm missing something but I don't understand how this would
eliminate the need for a huge switch statement. I'm trying to return
the most compact type possible so that:

It wouldn't completely, but it might reduce the hugeness of the switch.

(+ 1 1) => 2
(+ 1 1.1) => 2.1
(+ 1 3/4) => 7/4 (or something similar)

So, in my mind, the steps I need to take are this:
1) Determine which object is greater in the numerical tower.
2) Coerce the lesser object to the type of the greater one.
3) Add the two numbers.
4) Create a new object of the correct type and return the result.

Okay, consider this path:

1. Define your ordering.
2. Query the order of each object:
order1 = order_of(o1);
order2 = order_of(o2);
3. Pick the greater order:
use_this_order = (order1 > order2) ? order1 : order2;
4. Switch on it:
switch (use_this_order) {
case FLOAT_ORDER:
x = make_flonum(flonum_add(flonum_of(o1), flonum_of(o2)));
break;
case FIXNUM_ORDER:
x = make_fixnum(fixnum_add(fixnum_of(o1), fixnum_of(o2)));
break;
}

Do the calculation up front and use the results, instead of spreading
the logic all over.

Explicit rules like "if this is float and that is fix" (what your switch
statement does) are vulnerable to typos and thinkos; just do the general
rule all at once, expressed in the terms you put it in when you described
what you wanted to do. You don't want a million special rules; you want
the general rule "use the higher order".

You also want a thing that lets you, say, detect that something doesn't
fit the ordering and throw an error. :)

Am I missing something obvious here?

Maybe! Here's the great mystery:

It is (if you code for it) both cheap and harmless to coerce something to a
type it already has.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@xxxxxxxxx
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
.



Relevant Pages

  • Re: [Monad] Missing help about_switch
    ... Using a switch to handle multiple if statements. ... A switch statement is, in effect, a series of If statements. ... the automatic variable "$_" and is available during the evaluation of ... command at the MSH command prompt: ...
    (microsoft.public.windows.server.scripting)
  • Re: Switch for floating !!
    ... Why does the switch case statement does not have support ... characters representing elements of text, whose codes are arbitrarily assigned, ... Would you use a giant switch statement to distinguish fibonacci integers from ... You need a better example to actually rationalize switching on floating point ...
    (comp.lang.c)
  • Re: passing var args as-is to a function
    ... It would help to know why you want to avoid the switch() statement. ... corresponds directly to a switch statement, ... do_one(int x, int y); ... in which case the promoted type would ...
    (comp.lang.c)
  • Re: The Highest-Level Feature of C
    ... a normal switch statement. ... An alternative candidate for the highest level feature of C (and the one I ... The compiler is free to choose how to interface functions based on ...
    (comp.lang.c)
  • Re: switch vs. if
    ... Any half-decent compiler will reduce a switch statement to a jump ... a switch statement is conceptually much more like a jump table ...
    (comp.lang.cpp)