Differing levels of type-inference: Can D do this?
Simen Kjaeraas
simen.kjaras at gmail.com
Sun Jul 29 11:22:38 PDT 2012
On Sun, 29 Jul 2012 19:11:17 +0200, Chad J
<chadjoan at __spam.is.bad__gmail.com> wrote:
>> So there. Now, you simply use auto a = constrain!isInputRange(
>> expression );. Is this what you wanted?
>
> That's pretty good. It's still not as concise or easy to discover as
> the language's natural syntax for type declarations, but it's the kind
> of thing I'd use for my own purposes as a trick to get around language
> limitations.
It's likely as good as it gets without changing the language. Of course,
if you use this a lot with the same predicate, you could alias it to
something shorter:
alias constrain!isInputRange InputRange;
auto a = InputRange(expression);
Note also that the implementation supports multiple predicates, hence the
supplied hasElementType. And this is where aliases really come in handy:
template hasElementType( T ) {
template hasElementType( U ) {
enum hasElementType = is( ElementType!U == T );
}
}
alias constrain!(isInputRange, hasElementType!int) IntRange;
auto a = IntRange([1,2,3]);
--
Simen
More information about the Digitalmars-d-learn
mailing list