Extended Type Design: further examples

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Mon Mar 19 16:24:06 PDT 2007


Daniel Keep wrote:
> 
> Andrei Alexandrescu (See Website For Email) wrote:
>> (Reposted after cleanup :o))
>>
>> kris wrote:
>>> How about some further example? Given these sigs:
>>>
>>> struct Foo
>>> {
>>>    int a;
>>> }
>>>
>>> class Bar
>>> {
>>>    char[] b;
>>> }
>>>
>>> void fooish (inout Foo foo) {}
>>>
>>> char[] barish (Bar bar) {return bar.b;}
>>>
>>>
>>> 1) how do I modify the function decls to have the compiler *enforce*
>>> readonly upon the referenced content? e.g. Foo.a and Bar.b cannot be
>>> mutated within the function body?
>> void fooish (const ref Foo foo) {}
>>
>> char[] barish (const Bar bar) {return bar.b;}
>>
>>> 2) how does the fooish() decl change to have the compiler *enforce* a
>>> readonly-view of the returned bar.b?
>> const char[] barish (const Bar bar) {return bar.b;}
> 
> Hang on... wouldn't that need to be the signature of barish in the first
> place?  If const is transitive, bar is declared as a const Bar and
> you're returning bar.b, isn't bar.b const as well?

"Nobody said all examples must compile" -- Andrei

:o)

Andrei



More information about the Digitalmars-d mailing list