Extended Type Design.

Daniel Keep daniel.keep.lists at gmail.com
Fri Mar 23 16:56:01 PDT 2007



Bruno Medeiros wrote:
> Daniel Keep wrote:
>>
>> Bruno Medeiros wrote:
>>> Tyler Knott wrote:
>>>> Bruno Medeiros wrote:
>>>>> Huh? Wait a second, but won't D have the same issue, albeit with
>>>>> 'final'? For example, suppose you have:
>>>>>   final Foo foo;
>>>>> then what is:
>>>>>   typeof(&foo)
>>>>> ?
>>>>>
>>>> invariant Foo* or const Foo*, depending on whichever is most
>>>> appropriate for the situation.  I'd guess that invariant Foo* would be
>>>> the default because it's semantically closest to what is being
>>>> expressed (a pointer to immutable data), though const Foo* is also
>>>> allowable because you can't modify data through const pointers either.
>>> Let's suppose it's  const Foo*  (the less restrictive option). Even so,
>>> you will be restricting the type more than necessary, thus loosing some
>>> range of freedom. Because with  const Foo*  you can't change members of
>>> Foo*, while with final Foo, you can change members of foo. I.e.:
>>>
>>>   final Foo foo;
>>>   foo.x = 2;   // ok
>>>
>>>   typeof(&foo) fooptr = &foo;
>>>   (*fooptr.x) = 2;   // not allowed
>>
>> I guess the only way to solve that is to parameterise const and
>> invariant...
>>
>>> final Foo foo;
>>> final invariant Foo bar;
>>> writefln("%s", typeinfo(typeof(&foo)));
>>> writefln("%s", typeinfo(typeof(&bar)));
>>>
>>> --> invariant(1) Foo
>>>     invariant(*) Foo
>>
>> Honestly, I don't think that's going to happen :P
>>
>>     -- Daniel
>>
> 
> No, another way to solve that is to have 'final' be a type modifier as
> well:
> 
>   typeof(&foo)  --> (final Foo)*
> 
> This is essentially exactly like C++'s 'const'. (and like the 'rdonly'
> in my "hobbyist" design.)

I think the problem with that is that

  final pi = 3.1415...;

then doesn't do what you think it does.  If pi is now of type final
real, then you would still be able to re-assign it.

  final e = 2.14...;
  pi = e;

We probably *could* make it so that you can't assign to a variable
that's got a final'ed type, but then it's acting like a storage class,
not as a type.

I dunno.  This whole thread is just giving me a headache... I *thought*
I understood it after the first post, but now my head hurts and I'm not
so sure :P

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list