???Mindy operator overloading??? 
Author Message
 ???Mindy operator overloading???

Despite my reservations towards the "new" syntax, I've finally
retrieved the Mindy interpreter and begun exploring Dylan.  I like it.
I like it a lot.  Infix seems to be OK (of course, I haven't tried
writing a _macro_ yet)...

I do have a question about Mindy's operator overloading, however.  I
have attempted to overload the \- method for a class I'm playing with,
and it's not doing what I want it to.  Is this an implementation
problem or just a loose nut on my keyboard?

--- File: hexcoord.dylan ---

module: Hexcoord
rcs-header: $Header$

define library Hexcoord
  use Dylan;
  use Streams;
end;

define module Hexcoord
  use Dylan;
  use Extensions;
  use Streams;
  use Standard-IO;
end;

define class <hex-coord> (<object>)
  slot pack-u :: <integer>;
  slot pack-v :: <integer>;
end class;

define method initialize (hex :: <hex-coord>,
                          #key u: u = 0, v: v = 0, w: w = 0)
  pack-u := u - w;
  pack-v := v - w;
end method;

define method display (hex :: <hex-coord>)
  let (u, v, w) = hex.natural;
  puts ("#<");
  prin1(u); puts(","); prin1(v); puts(","); prin1(w);
  puts (">\n");
end method;

define method natural (hex :: <hex-coord>)
 => (u, v, w);
  let u = 0;
  let v = 0;
  let w = 0;
  case
    (pack-u >= 0 & pack-v <= 0) | (pack-u <= 0 & pack-v >= 0) =>
      u := pack-u;
      v := pack-v;
    abs (pack-u) >= abs (pack-v) =>
      u := pack-u - pack-v;
      w := -pack-v;
    otherwise =>
      v := pack-v - pack-u;
      w := -pack-u;
  end case;
  values (u, v, w);
end method;

define method \- (lh :: <hex-coord>, rh :: <hex-coord>)
 => result :: <hex-coord>;
  let (lu, lv, lw) = lh.natural;
  let (ru, rv, rw) = rh.natural;
  print(lu);                     // 7 (should be 10!?!)
  print(ru);                     // 7
  print(lv);                     // 0
  print(rv);                     // 0
  print(lw);                     // 0 (should be -1!?!)
  print(rw);                     // 0
  make (<hex-coord>, u: lu - ru, v: lv - rv, w: lw - rw);
end method;

define method main (#rest ignored)
  let lh = make (<hex-coord>, u: 10, w: -1);
  let rh = make (<hex-coord>, u: 7);
  let hex = lh - rh;
  display (hex);                 // #<0,0,0> (should be #<3,0,-1>)
end method;

--- EOF: hexcoord.dylan ---
--
// Kaelin Colclasure ---------------------------------------------------------

// Voice: (314)567-8463                      717 Office Parkway
//   Fax: (314)432-5391                      St. Louis, MO 63141



Sun, 30 Mar 1997 12:43:41 GMT  
 ???Mindy operator overloading???

Quote:

> Is this an implementation problem or just a loose nut on my keyboard?

One major problem is that you aren't accessing instance variables correctly:

Quote:
>define method natural (hex :: <hex-coord>)
> => (u, v, w);
>  case
>    (pack-u >= 0 & pack-v <= 0) | (pack-u <= 0 & pack-v >= 0) =>

You need to say "hex.pack-u".  pack-u would normally be the accessor function
itself, except that you probably clobbered it with an integer in the initialize
method when you said:

Quote:
>define method initialize (hex :: <hex-coord>,
>                      #key u: u = 0, v: v = 0, w: w = 0)
>  pack-u := u - w;

A more subtle problem is that your initialize method doesn't call next-method,
which all initialize methods should do as the first thing (in probably doesn't
matter in this case.)

Any real Dylan (and probably even Mindy) should have complained when you
clobbered the slot accessor function, as it is supposed to be a constant.

  Rob



Mon, 31 Mar 1997 00:16:57 GMT  
 ???Mindy operator overloading???


   > Is this an implementation problem or just a loose nut on my keyboard?

   One major problem is that you aren't accessing instance variables correctly:
   >define method natural (hex :: <hex-coord>)
   > => (u, v, w);
   >  case
   >    (pack-u >= 0 & pack-v <= 0) | (pack-u <= 0 & pack-v >= 0) =>

   You need to say "hex.pack-u".  pack-u would normally be the accessor function
   itself, except that you probably clobbered it with an integer in the initialize
   method when you said:

   >define method initialize (hex :: <hex-coord>,
   >                   #key u: u = 0, v: v = 0, w: w = 0)
   >  pack-u := u - w;

I _knew_ that.  Sigh.  This business of choosing a syntax that's
closer to what "the masses" are already familiar with has its
drawbacks.  I kinda backslid into C++-mode there... ;-)

   A more subtle problem is that your initialize method doesn't call next-method,
   which all initialize methods should do as the first thing (in probably doesn't
   matter in this case.)

Thanks for the tip...

   Any real Dylan (and probably even Mindy) should have complained when you
   clobbered the slot accessor function, as it is supposed to be a constant.

I agree, but in all fairness this limitation _is_ pointed out in the
Mindy documentation (ie. constants aren't).  Thanks for helping me out!
--
// Kaelin Colclasure ---------------------------------------------------------

// Voice: (314)567-8463                      717 Office Parkway
//   Fax: (314)432-5391                      St. Louis, MO 63141



Mon, 31 Mar 1997 02:17:21 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. operator overloading and user definable operators.

2. Overloading logical operators and bitwise operators

3. Operator overload of base operator and compiler diagnostic

4. operator overloading

5. Errorhadler- overload operators (how to?)

6. Overloading operators with Clipper

7. VO: Operator overloading (babble)

8. Forth with arbitrary objects (operator overloading)

9. Overloading of arithmetic and equality operators

10. Any way to do operator overloading in Dylan?

11. Operator overloading?

12. Operator overloading?

 

 
Powered by phpBB® Forum Software