Walter's second axiom

Bill Baxter dnewsgroup at billbaxter.com
Tue Dec 11 16:13:38 PST 2007


Jason House wrote:
> Walter Bright wrote:
>> That axiom didn't really exist until Andrei started pointing out to me
>> all the advantages of being able to wrap types in a struct. So we can
>> expect some historical problems with it.
> 
> Can you please share with us all the advantages of "Any type T can be
> wrapped inside a struct S, and that S can be made to behave as a typedef
> for type T."
> 
> 
> Under what conditions can it behave exactly like a typedef?  If it behaves
> *exactly* as a typedef under all circumstances, then it can't have any
> additional data members or member functions and must behave exactly like
> the original.  If that's true, why wrap it in a struct at all?
> 
> I assume there has to be some way to bend that.  Candidates in my mind are
> extra data members and extra member functions.  Extra data members seems
> less likely because a cast from S to T and back to S would lose data
> (something that typedef's don't have happen).  Of course, this may fit into
> the grand scheme of opImplicitCast.
> 
> I also wonder if extending a class may be an alternative to wrapping it in a
> struct.  Maybe wrapping in a struct could be restricted to value types?  I
> just don't understand this enough yet to give any credible suggestions.

I don't think the idea is to restrict it to the original set of methods. 
  Adding or removing methods is ok.  For instance if you want to make 
something that's like an array but can't have its length changed then 
then just leave out the .length setter property, etc.

Even if you keep the exact same interface as the original type it's 
still useful to be able to intercept methods to provide notifications. 
For instance in a GUI program you could create a float wrapper that acts 
just like a regular float, except whenever you set or change its value 
it automatically sends out notifications to anyone who's interested in 
the value.  Or maybe you're writing some distributed processing thing 
and setting the value is going to cause packets to be sent out to 
another process over the net that is mirroring this one's state.

--bb



More information about the Digitalmars-d mailing list