[Nickle]I think I've finally got it (was: I'm confused about type promotion)

Carl Worth nickle@nickle.org
Thu, 25 Jul 2002 00:59:50 +0000


Keith,

Many thanks for all the patient explanations.

On Jul 24, Keith Packard wrote:
 > I suspect one source of your confusion is the super/sub type relations
 > among the numeric and structured types.

This is exactly what had me tripped up. There's a substantive
difference between these two relations, (in the way the values of the
sub-type are constrained as compared to the super-type).

 >  A super type is a type which can represent all of the values of
 > it's subtypes, plus some additional values.

This always made sense to me, but I had to read the following
description of the super/sub relation among structured types about a
dozen times before realizing it didn't contradict this definition, (it
seemed to me that adding fields allowed more values not fewer -- but I
finally figured it out).

My confusion came from imagining trying to do something like the
following with structured values:

	typedef struct {
		real r;
	} Real;

	typedef struct {
		real r;
		real i;
	} Complex;

Which yields Complex as a sub-type of Real. With these definitions
it's always legal to assign a Complex value to a variable of type
Real, and it's only legal to assign from a Real variable to a Complex
variable if the Real variable happens to be holding values for both r
and i.

Those aren't the most useful semantics for this situation of course.

Instead, if the numeric types included a complex type, it would
actually be a super-type of real. Then it would always be legal to
assign a real value to a variable of type complex, and only legal to
assign from a complex variable to a real variable if the complex
variable happens to be holding a real value.

Those would be the ideal semantics for dealing with real and complex
types/values, but structured value types don't allow for the same kind
of power that the builtin numeric types have.

-Carl

-- 
Carl Worth                                        
USC Information Sciences Institute                 cworth@east.isi.edu
3811 N. Fairfax Dr. #200, Arlington VA 22203		  703-812-3725