Returning const? -- A potential solution

Tim M a at b.com
Sat Mar 7 22:22:44 PST 2009


On Sun, 08 Mar 2009 19:18:52 +1300, Daniel Keep  
<daniel.keep.lists at gmail.com> wrote:

>
> If you're not actually responding to a post, please don't quote the
> entire thing in your message.
>
> Tim M wrote:
>> What does this mean:
>>
>> module tconst;
>>
>> import std.stdio;
>>
>> invariant(char)[] func()
>> {
>>       invariant(char)[] s = "hello";
>>       return s;
>> }
>>
>> void main()
>> {
>>       auto s = func();
>>       s[0] = 'm'; //error
>> }
>>
>> I thought we already have returning const/invariant? That code ^ works
>> fine for me.
>
> You missed the point.  This has nothing to do with returning invariant
> types.  Jason is proposing a way to create a function which maintains
> the const-ness of its arguments without having to implement multiple
> versions.  In other words,
>
> return(T) max(return(T) a, return(T) b){ return (a>b)?a:b; }
>
> Would be similar to the following:
>
> T max(T a, T b){ return (a>b)?a:b; }
> const(T) max(const(T) a, const(T) b){ return (a>b)?a:b; }
> invariant(T) max(invariant(T) a, invariant(T) b){ return (a>b)?a:b; }
>
> Except that each would share a single implementation.
>
>   -- Daniel




More information about the Digitalmars-d mailing list