Returning const? -- A potential solution

Daniel Keep daniel.keep.lists at gmail.com
Sat Mar 7 22:18:52 PST 2009


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