Constraints error messages [Was: Re: Constrained Templates]

bearophile bearophileHUGS at lycos.com
Mon Jun 14 13:37:50 PDT 2010


Yao G.:

> Something I would like to see with template constraints, is the ability to  
> show text messages, like when static if is used.

This is a problem I too have.
In D1 I used to write code like:

template Foo(T) {
  static assert(condition1, "error message1");
  static assert(condition2, "error message2");  
  ...
}


Now in D2 I usually write:

template Foo(T) if (condition1 && condition2) {
  ...
}


This D2 code is better because error compiler messages are better (at the template instantiation point instead inside the template body), but it's worse because I lose the error messages written by me. In simple situations the error messages are not so important, but in some situations they are useful to understand why the template input values are wrong.

A syntax like the following allows to add the user-defined error messages too, but it's bad looking, so probably something quite better can be invented:

template Foo(T)
  if (condition1) else pragma(msg, "error message1")
  if (condition2) else pragma(msg, "error message2") {
}

Bye,
bearophile


More information about the Digitalmars-d mailing list