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) {
x = make_flonum(flonum_add(flonum_of(o1), flonum_of(o2)));
x = make_fixnum(fixnum_add(fixnum_of(o1), fixnum_of(o2)));

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.

Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@xxxxxxxxx <-- lawsuits, religion, and funny pictures <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.