Proposal for scoped const contracts
Steven Schveighoffer
schveiguy at yahoo.com
Wed Mar 26 08:32:27 PDT 2008
"Oliver Dathe" wrote
> The following is some kind of superset of what I have proposed in [1]
>
> Example for syntax (*):
>
> char[] f(char[] p : const(char)[]) { }
> ^ ^
> Least restrictive type Tmin |
> Contract type Tcontract
>
> Basic discussion:
> a.) f may only compile if it accomplishes the contract regarding p.
> b.) It is an error if Tmin is not implicitly castable to Tcontract.
> c.) For the syntax regarding the contract I used ":" at the moment. For
> further syntax proposals, see (*)
> d.) At the moment we may call the example f with parameters of type char[]
> and const(char)[]. const(char[]) is not covered by the contract and
> therefor may have to be rejected. More on that (**)
> e.) It is still nothing said what the typeof(p) is inside the function. I
> assume it has to be the type of the passed parameter.
> f.) If we want to return some slice of p, we get stuck again. Walter and
> Andrei have presented a solution in [2] up to some degree (***).
> char[] f(return char[] p : const(char)[]) { return p[17..42]; }
> g.) f can be virtual, because no templates are required.
I'm not exactly sure what this accomplishes, because of point f) above, this
seems like it is the same as:
char[] f(const(char)[] p) {...}
Meaning, f cannot modify p or return a slice of p.
-Steve
More information about the Digitalmars-d
mailing list