static interface
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Tue Nov 17 00:54:46 PST 2009
Bill Baxter wrote:
> On Mon, Nov 16, 2009 at 9:25 AM, Leandro Lucarella <llucax at gmail.com> wrote:
>
> This topic is actually very close to a discussion last week about
> retrieving error messages from failures of __traits(compiles, xxx).
>
> My question is: is it really that much of an improvement?
>
> I've rearranged your code to see the equivalent snippets side-by-side:
>
>> static interface InputRange(T) {
>> bool empty();
>> T front();
>> void popFront();
>> }
>
>
>> template isInputRange(R)
>> {
>> enum bool isInputRange = is(typeof(
>> {
>> R r;
>> if (r.empty) {}
>> r.popFront;
>> auto h = r.front;
>> }()));
>> }
>
> There's actually not that much difference here. Of course we would
> like several general improvements that have been discussed before:
> 1) some kind of template 'this' so we don't have to repeat the template name.
> 2) something better than is(typeof()) (or __traits(compiles, ...)) to
> check if code is ok. [hoping for meta.compiles(...)]
>
> In some ways the current code is better, because it actually checks if
> a construct works or not, rather than requiring a specific function
> signature. Whether the code will work is really the minimal
> restriction you can place on the interface. A specific may be too
> tight. For instance, above you don't really care if empty returns
> bool or not. Just if you can test it in an "if".
Another, related way in which the current code is better is that it
doesn't care whether empty, front, and popFront are functions or
variables. As we know, the standard trick for infinite ranges is to
declare empty as an enum, not a function.
BTW, I find it interesting that I see meta.* being mentioned in
discussions and even used in code snippets all over the NG these days.
It seems to be a popular proposal. I hope it gets implemented too. :)
-Lars
More information about the Digitalmars-d
mailing list