Tuple IFTI with implicit conversions

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Sun Oct 21 01:46:55 PDT 2007


Reiner Pope wrote:

> In their simplest form, predicates solve a different problem. To give a
> concrete example of what I'm talking about, suppose I have a struct:
> 
> struct Wrapper(T)
> {
>      const bool IsWrapper = true;
>      ...
>      static Wrapper!(T) opImplicitCastFrom(T t) {...}
> }
> 
> and I have a function which requires that it is called with arguments
> always wrapped in Wrapper (Wrapper!(T) might, for instance, be lazy(T),
> or a reference to T, so that it has to be wrapped on the calling side).
> 
> Suppose you had a predicate which said if some type, T, is a Wrapper of
> something else. It might look like:
> 
> template WrapperPredicate(T)
> {
>      const bool WrapperPredicate = is(T.IsWrapper == bool);
> }
> 
> If you then wrote a templated function using this predicate, it wouldn't
> work:
> 
> void foo(T : WrapperPredicate!(T)) (T t) {}
> 
> void main() {
>      foo(5); // error, int is not a Wrapper, so no template found
> }
> 
> This seems to be where you say polysemous values help, ie when
> instantiating the template, you come up with a list of possible types
> for T, and see if any match the predicate. The problem is that this list
> can be infinitely long

Agreed, calculation of the lists might become intractable. On the other hand
you could extend the predicate here to accept both, wrappers and
primitives. Then the intractability would depend on the search method.

> However, I agree with you about the need for predicates. There's a neat
> trick which allows you to use them already in D, though:
> 
> void foo(T, bool T_predicate : true = IsValidParam!(T)) (T t) {...}

Yep, and this syntax seems to make more sense since it allows you to specify
predicates independently of the other template parameters.

> It works well, except that it doesn't allow specialization.

True (at least in the case of IFTI), but I meant the predicate would allow
specialization with and without IFTI. It would also use similar semantics
to IsExpression:

void foo(T)(T T) { ... } // 1)
void foo(T, isValidParam!(T) == true)(T t) { ... } // 2)

Now when isValidParam!(T) evaluates to true, 2) would be selected, otherwise
1). Again the syntax isn't probably as good as it could be.



More information about the Digitalmars-d mailing list