[Prev][Next][Index][Thread]

Re: 'indefinite' element types



Dustin Voss d_voss@uswest.net on  2000-07-17 23:15 wrote:

> Look at <simple-object-vector>.  Its element type is "<object>", but you
> can set and get elements of any type -- not just <object>.  In what way
> is that different from <simple-vector>, whose element type is
> "indefinite <= <object>"?

I tried to outline the difference in my original message.  It's hard to see
the difference though when the element type is <object>, since everything is
a subtype of <object>.  If we assume that limited could work on user-defined
types, I think the following shows the difference between collections whose
element type is "indefinite <= <some-type>" and "<some-type>".

[You hit the nail on the head with your example of how to create a
collection with type indefinite <= <something-other-than-object>.  I just
wish there were a more concise way to do it, as I think it would sometimes
be useful]

|| A vector whose elements are indefinite <= <real>
define class <indefinite-real-vector> (<vector>) end;
define method element(v :: <indefinite-real-vector>) =>
  <real>
  next-method();
end;
define method element-setter(new :: <real>,
                             v :: <indefinite-real-vector>)
  next-method();
end;

|| A subtype of that whose elements are guaranteed to be <real>
define constant <limited-real-vector> =
  limited(<indefinite-real-vector>, element-type: <real>);

|| A subtype of that, whose elements are guaranteed to be <integer>
define constant <limited-integer-vector> =
  limited(<indefinite-real-vector>, element-type: <integer>);

|| Note the following
subtype?(<limited-integer-vector>, <indefinite-real-vector>) => #t
subtype?(<limited-real-vector>, <indefinite-real-vector>) => #t
subtype?(<limited-integer-vector>, <limited-real-vector>) => #f

define function runtime-check(v :: <indefinite-real-vector>)
  || Must check that v can hold floats at run time
  v[0] := 4.2;
end;

define function compiletime-check(v :: <limited-real-vector>)
  || v is guaranteed to be able to hold floats
  v[0] := 4.2;
end;




Follow-Ups: References: