Proposal for scoped const contracts
Janice Caron
caron800 at googlemail.com
Tue Mar 25 01:39:54 PDT 2008
The more I think about this, the more I find I have to agree with
Stephen completely. I think I might be able to come up with a better
syntax though.
Instead of
out(T) min(T)(in(T) x, in(T) y)
{
return x < y ? x : y;
}
which is what Stephen suggested, I think this works better:
inout(U=T) U min(T)(U x, U y)
{
return x < y ? x : y;
}
It's a pretty straightforward enhancement really. Just preceed a
function definition with the attribute inout(U=T), and then within the
function definition, all U's are in/out types. Here's the same idea
used with strstr
inout(U=char) U[] strstr(U[]s, const(char)[] pattern)
{
int n = s.find(pattern);
return n == -1 ? null : s[n..$];
}
The reason I like this better is that it means we only have to define
the "in/out" type once, instead of multiple times. Also - it means we
don't have to ditch the existing uses of "in" and "out".
We can also use it for member functions, except with a slightly modified syntax
class String
{
inout(this) strstr(const(String) pattern)
{
/*...*/
}
}
I like the idea of only having to express the inout type only once per
function declaration, and I like the idea of not having to completely
change the existing meanings of "in" and "out" in a way which would
break old code.
Another advantage of my syntax is that you could use inout(U=T) as an
attribute to bracket multiple functions. e.g.
inout(U=char)
{
U[] strchr(U[]s, char c);
U[] strstr(U[]s, const(char)[] pattern);
}
Stephen, what do you think? Have I missed anything?
More information about the Digitalmars-d
mailing list