Fun with templates
TommiT
tommitissari at hotmail.com
Sat Jul 6 06:50:41 PDT 2013
On Saturday, 6 July 2013 at 13:30:02 UTC, TommiT wrote:
> Some more details of this proposed feature:
>
> inout(T) foo(inout T)(T a, T b)
> {
> return var;
> }
>
> void bar(inout T)(T a, T b)
> {
> a = b;
> }
>
> void main()
> {
> const int con;
> immutable int imm;
>
> foo(con, con); // OK (returns const int)
> foo(con, imm); // Error: returned inout(T) is ambiguous
>
> bar(con, con); // OK
> bar(con, imm); // OK (no ambiguity because inout not used)
> }
Another approach, if we wanted a different syntax, would be to
add a new keyword 'mutable'. Like immutable overrides const,
mutable would override both const and immutable:
static assert(is(mutable(const(int)) == int));
static assert(is(mutable(immutable(int)) == int));
Here's how it would look:
T foo(T)(mutable(T) a, mutable(T) b)
{
a = b = 123;
return a;
}
void main()
{
const int con;
immutable int imm;
foo(con, con); // OK (returns const int)
foo(con, imm); // Error: T is ambiguous
}
More information about the Digitalmars-d
mailing list