Re: Constraints error messages [Was: Re: Constrained Templates]

2010-06-15 Thread bearophile
BCS:

>I like the idea, but how should it interact with overloaded templates?<

The syntax/semantics I have shown is just a starting point, there are probably 
way better ways to reach the same goal. For example a kind of run-time 
error/exception for templates that shows an error message and removes the 
template from the list of possible templates.

Regarding overloaded templates, those else clauses are meant to be useful 
mostly when there is no template overloading, they are designed to be useful 
not for the usual purpose of constrains, that is to remove templates from the 
pool of overloads, but mostly when you have a single template that has input 
types and values that must satisfy some requirements. So it's a grubby idea 
that needs more thinking :-)

Bye,
bearophile


Re: Constraints error messages [Was: Re: Constrained Templates]

2010-06-14 Thread BCS

Hello bearophile,


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") {
}


I like the idea, but how should it interact with overloaded templates?

template Foo(T) if(cond1!T) else pragma(msg, "a") {}
template Foo(T) if(cond2!T) else pragma(msg, "b") {}

Don't run any of the elses unless all the options fail and then run all of 
them?


--
... <





Constraints error messages [Was: Re: Constrained Templates]

2010-06-14 Thread bearophile
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


Re: Constrained Templates

2010-06-14 Thread Simen kjaeraas

Don  wrote:


This can be trivially shown to be NP-complete.

void bar(T)()
{
static if ( big_function!T) {
  T t;
  t.flabbergast( );
}
}

Compiler cannot determine if T needs flabbergast(), unless it evaluates  
big_function. Which can be arbitrarily complicated.


But how does the compiler decide whether or not to include that code
usually? Would it not then also have to evaluate big_function?


We're taking the approach of making the default error message in such  
cases as helpful as possible.


Which is likely a better choice. The idea just hit me, and I decided
to probe for reactions.

--
Simen


Re: Constrained Templates

2010-06-14 Thread Simen kjaeraas

Jason House  wrote:


Simen kjaeraas Wrote:


Leandro Lucarella  wrote:
> it would be nice
> to have some sort of way to tell the compiler to write the template
> constraints for us (the obvious ones at least, there might be other
> template constraints desired besides the ones the ones the compiler  
can

> figure out). This way, the errors can be improved without user
> intervention.

How's about @optional? Marks an entire function as optional, i.e. will
not be included if it does not compile.

struct foo( T ) {
   @optional
   void bar( ) { // Will not exist if T cannot be flabbergasted.
 T tmp;
 t.flabbergast( );
   }
}

--
Simen


Would it be a bug or a feature if bar was never included? As written,  
bar should never compile...


Well spotted. And a very good reason not to have this feature (or to
write unit tests, I guess :p )

--
Simen


Re: Constrained Templates

2010-06-14 Thread Don

Simen kjaeraas wrote:

Leandro Lucarella  wrote:

it would be nice
to have some sort of way to tell the compiler to write the template
constraints for us (the obvious ones at least, there might be other
template constraints desired besides the ones the ones the compiler can
figure out). This way, the errors can be improved without user
intervention.


How's about @optional? Marks an entire function as optional, i.e. will
not be included if it does not compile.

struct foo( T ) {
  @optional
  void bar( ) { // Will not exist if T cannot be flabbergasted.
T tmp;
t.flabbergast( );
  }
}



This can be trivially shown to be NP-complete.

void bar(T)()
{
   static if ( big_function!T) {
 T t;
 t.flabbergast( );
   }
}

Compiler cannot determine if T needs flabbergast(), unless it evaluates 
big_function. Which can be arbitrarily complicated.


We're taking the approach of making the default error message in such 
cases as helpful as possible.




Re: Constrained Templates

2010-06-14 Thread Yao G.
:D That's weird. I just filled some typical form controls and clicked  
submit. No "autoban" button was clicked.


Yao G.

On Sun, 13 Jun 2010 20:05:04 -0500, Walter Bright  
 wrote:



Yao G. wrote:

It is now displayed in the front page.
 http://www.reddit.com/r/programming/
 On Sun, 13 Jun 2010 15:42:13 -0500, Walter Bright  
 wrote:



Yao G. wrote:

Maybe I did something wrong at the moment I submitted the article.  :(


I sent an email to the reddit moderators asking what's up with that.




Here's the email I received from the moderators: "Fixed, it was  
autobanned." It didn't say what rule was used to autoban it.



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


Re: Constrained Templates

2010-06-14 Thread Jason House
Simen kjaeraas Wrote:

> Leandro Lucarella  wrote:
> > it would be nice
> > to have some sort of way to tell the compiler to write the template
> > constraints for us (the obvious ones at least, there might be other
> > template constraints desired besides the ones the ones the compiler can
> > figure out). This way, the errors can be improved without user
> > intervention.
> 
> How's about @optional? Marks an entire function as optional, i.e. will
> not be included if it does not compile.
> 
> struct foo( T ) {
>@optional
>void bar( ) { // Will not exist if T cannot be flabbergasted.
>  T tmp;
>  t.flabbergast( );
>}
> }
> 
> -- 
> Simen

Would it be a bug or a feature if bar was never included? As written, bar 
should never compile...


Re: Constrained Templates

2010-06-14 Thread Simen kjaeraas

Leandro Lucarella  wrote:

it would be nice
to have some sort of way to tell the compiler to write the template
constraints for us (the obvious ones at least, there might be other
template constraints desired besides the ones the ones the compiler can
figure out). This way, the errors can be improved without user
intervention.


How's about @optional? Marks an entire function as optional, i.e. will
not be included if it does not compile.

struct foo( T ) {
  @optional
  void bar( ) { // Will not exist if T cannot be flabbergasted.
T tmp;
t.flabbergast( );
  }
}

--
Simen


Re: Constrained Templates

2010-06-13 Thread Andrei Alexandrescu

Walter Bright wrote:

Yao G. wrote:

It is now displayed in the front page.

http://www.reddit.com/r/programming/

On Sun, 13 Jun 2010 15:42:13 -0500, Walter Bright 
 wrote:



Yao G. wrote:

Maybe I did something wrong at the moment I submitted the article.  :(


I sent an email to the reddit moderators asking what's up with that.





Here's the email I received from the moderators: "Fixed, it was 
autobanned." It didn't say what rule was used to autoban it.


This is odd. Now my post on tech talk tips has vanished from the new 
articles list.


Andrei


Re: Constrained Templates

2010-06-13 Thread Nick Sabalausky
"Andrei Alexandrescu"  wrote in message 
news:hv46bv$1kp...@digitalmars.com...
> Walter Bright wrote:
>> Nick Sabalausky wrote:
>>> "and a line number pointing into the template body. The error is being 
>>> reported as occurring somewhere other than where the actual problem is"
>>>
>>> That would be a good place to point out that recent versions of DMD 
>>> display template instantiation traces upon template instatiation errors 
>>> (Does C++ do that?). Then, of course, it could go on to "but better 
>>> yet...".
>>
>> I wished to avoid quality of implementation issues, and instead focus on 
>> characteristics of the language.
>
> I think Nick does make a valid point: there is access to the error, it 
> just takes some more info from the compiler.
>

My concern was that C++ users (or other non-D-users) might get the mistaken 
impression that without constraints, *all* they would get is the unhelpful 
"Error: incompatible types for ((a) % (b)): 'int*' and 'int*'", and 
re-enforce their belief that templates have an innate tendency to give 
useless error messages, and that if you use D, constraints are the only help 
you get.

But I may be over-thinking it and worrying about nothing.




Re: Constrained Templates

2010-06-13 Thread Andrei Alexandrescu

Walter Bright wrote:

Nick Sabalausky wrote:
"and a line number pointing into the template body. The error is being 
reported as occurring somewhere other than where the actual problem is"


That would be a good place to point out that recent versions of DMD 
display template instantiation traces upon template instatiation 
errors (Does C++ do that?). Then, of course, it could go on to "but 
better yet...".


I wished to avoid quality of implementation issues, and instead focus on 
characteristics of the language.


I think Nick does make a valid point: there is access to the error, it 
just takes some more info from the compiler.


Andrei


Re: Constrained Templates

2010-06-13 Thread Andrei Alexandrescu

Andrei Alexandrescu wrote:

Walter Bright wrote:

Yao G. wrote:

Maybe I did something wrong at the moment I submitted the article.  :(


I sent an email to the reddit moderators asking what's up with that.


I posted this as a test:

http://www.reddit.com/r/reddit.com/comments/cemtc/tech_talk_tips/

Andrei


Sorry I was on the plane and in a hurry so I posted to general reddit, 
not programming. I now deleted that (sorry BCS) and now reposted under 
programming. Vote me up please :o).


http://www.reddit.com/r/programming/comments/ceo9s/tech_talk_tips/

The point of the experiment was that Walter had hypothesized it's 
possible his name is on a scrutiny/spam list. I removed his name from 
the article and the results are mixed: when I posted in the general 
reddit community the article didn't make it on the new articles list, 
however this latest post in /programming/ was listed instantly. In the 
meantime I saw that Walter's constrained templates article appeared but 
with a delay. Go figure.



Andrei


Re: Constrained Templates

2010-06-13 Thread Walter Bright

Nick Sabalausky wrote:
"and a line number pointing into the template body. The error is being 
reported as occurring somewhere other than where the actual problem is"


That would be a good place to point out that recent versions of DMD display 
template instantiation traces upon template instatiation errors (Does C++ do 
that?). Then, of course, it could go on to "but better yet...".


I wished to avoid quality of implementation issues, and instead focus on 
characteristics of the language.


Re: Constrained Templates

2010-06-13 Thread Walter Bright

Yao G. wrote:

It is now displayed in the front page.

http://www.reddit.com/r/programming/

On Sun, 13 Jun 2010 15:42:13 -0500, Walter Bright 
 wrote:



Yao G. wrote:

Maybe I did something wrong at the moment I submitted the article.  :(


I sent an email to the reddit moderators asking what's up with that.





Here's the email I received from the moderators: "Fixed, it was autobanned." It 
didn't say what rule was used to autoban it.


Re: Constrained Templates

2010-06-13 Thread BCS

Hello Nick,


"Walter Bright"  wrote in message
news:hv39tt$87...@digitalmars.com...


http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html

Anyone want to do the honors and post to reddit, ycombinator, etc. ?


"and a line number pointing into the template body. The error is being
reported as occurring somewhere other than where the actual problem
is"

That would be a good place to point out that recent versions of DMD
display template instantiation traces upon template instatiation
errors (Does C++ do that?).


I think GCC does. But IIRC it ends up being an almost unreadable one.


---
Not sent from an iPhone.

--
... <





Re: Constrained Templates

2010-06-13 Thread BCS

Hello Ali,



The method that I learned from Phobos is not the best, but at least
the function interface reads better:

T gcd(T)(T a, T b)
if (supports_modulus!T)
{
T result;
// ...
return result;
}
That makes it clear that T must support the modulus operation.

Here is how supports_modulus may be defined:

template supports_modulus(T)
{
const bool supports_modulus = is (typeof(
{
T a;
T b;
T c = a % b;
}()));
}


this shorter vertion also works:

template supports_modulus(T)
{
const bool supports_modulus = is (typeof(T.init%T.init) == T);
}

--
... <





Re: Constrained Templates

2010-06-13 Thread Steven E. Harris
Leandro Lucarella  writes:

> When you get used to this idiom, it might not look to bad, but I'm
> seeing people new to D wonder "why in the hell was that syntax used?"
> while they try to decode what is(typeof()) means.

The syntax doesn't bother me as much as the suggestion to repeat details
of the template's implementation. C++'s concepts were going in this
direction too, adding code that suffers the same problem as
documentation: It can fall out of step with "the real code", and keeping
it aligned requires repeating details that should only be stated once --
in "the real code".

Here we're debating how to state that some type can participate in a
modulus operation, but that we even need to apply this operation to the
function's arguments is an internal detail. It could change after the
first pass at implementing the function, adopting a different algorithm,
and we could wind up continuing to impose requirements on the types that
are no longer relevant to the revised implementation. It looks like yet
another maintenance burden.

-- 
Steven E. Harris


Re: Constrained Templates

2010-06-13 Thread Nick Sabalausky
"Walter Bright"  wrote in message 
news:hv39tt$87...@digitalmars.com...
> http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html
>
> Anyone want to do the honors and post to reddit, ycombinator, etc. ?

"and a line number pointing into the template body. The error is being 
reported as occurring somewhere other than where the actual problem is"

That would be a good place to point out that recent versions of DMD display 
template instantiation traces upon template instatiation errors (Does C++ do 
that?). Then, of course, it could go on to "but better yet...".

---
Not sent from an iPhone.




Re: Constrained Templates

2010-06-13 Thread Yao G.

It is now displayed in the front page.

http://www.reddit.com/r/programming/

On Sun, 13 Jun 2010 15:42:13 -0500, Walter Bright  
 wrote:



Yao G. wrote:

Maybe I did something wrong at the moment I submitted the article.  :(


I sent an email to the reddit moderators asking what's up with that.



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


Re: Constrained Templates

2010-06-13 Thread Andrei Alexandrescu

Walter Bright wrote:

Yao G. wrote:

Maybe I did something wrong at the moment I submitted the article.  :(


I sent an email to the reddit moderators asking what's up with that.


I posted this as a test:

http://www.reddit.com/r/reddit.com/comments/cemtc/tech_talk_tips/

Andrei


Re: Constrained Templates

2010-06-13 Thread Ali Çehreli

Leandro Lucarella wrote:
> Walter Bright, el 13 de junio a las 12:01 me escribiste:
>> http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html
>>
>> Anyone want to do the honors and post to reddit, ycombinator, etc. ?
>
> Nice article, but when I read:
>
>T gcd(T)(T a, T b)
>if (is(typeof(a % b)))
>{
>...
>}
>
> Under the presence of such beauty as template constraint syntax is,
> "is(typeof(a % b))" makes my eyes hurt, and my brain wonder.

The method that I learned from Phobos is not the best, but at least the 
function interface reads better:


T gcd(T)(T a, T b)
if (supports_modulus!T)
{
T result;
// ...
return result;
}

That makes it clear that T must support the modulus operation.

Here is how supports_modulus may be defined:

template supports_modulus(T)
{
const bool supports_modulus = is (typeof(
{
T a;
T b;
T c = a % b;
}()));
}

Here are what is at play to get that "named constraint":

- The curly braces after the typeof define a closure (delegate?) literal

- The body includes how the type should be used to satisfy the 
'supports_modulus' constraint


- The closure is "executed" with the empty parethesis at the end of it 
(actually, not executed at all; because the expression is a parameter to 
typeof, which never executes its argument)


- [This is my assumption] typeof produces an invalid type for invalid 
expressions


- One of the uses of the 'is' expression produces a bool result if the 
type that it receives is not valid


- When a template contains just one definition and that definition 
matches the name of the template, the template instantiation is the same 
as the definition that it contains. i.e. instead of writing


(supports_modulus!T).supports_modulus

to mean the bool value, one merely writes

 supports_modulus!T

A lot of D features! :) But once that code structure is accepted as an 
idiom, it works and gives names to constraints.


Ali


Re: Constrained Templates

2010-06-13 Thread Walter Bright

Yao G. wrote:

Maybe I did something wrong at the moment I submitted the article.  :(


I sent an email to the reddit moderators asking what's up with that.


Re: Constrained Templates

2010-06-13 Thread Yao G.

Maybe I did something wrong at the moment I submitted the article.  :(

Somebody else should try to submit it too.

On Sun, 13 Jun 2010 15:15:25 -0500, Walter Bright  
 wrote:



Yao G. wrote:

http://www.reddit.com/r/programming/comments/cel4o/constrained_templates_in_the_d_programming/


Thanks!

Anyone know why this doesn't appear on the "new" page?

http://www.reddit.com/r/programming/new/



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


Re: Constrained Templates

2010-06-13 Thread Walter Bright

Yao G. wrote:
http://www.reddit.com/r/programming/comments/cel4o/constrained_templates_in_the_d_programming/ 


Thanks!

Anyone know why this doesn't appear on the "new" page?

http://www.reddit.com/r/programming/new/


Re: Constrained Templates

2010-06-13 Thread Yao G.
Andrei used that idiom quite extensive in Phobos, unfortunately, I dare to  
say. It makes the code a lot harder to read. But the alternative, using  
__traits(compiles, ... ) is even worse IMO, because even as is a little  
more obvious, is quite verbose.


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


Yao G.

On Sun, 13 Jun 2010 14:36:14 -0500, Leandro Lucarella   
wrote:



Walter Bright, el 13 de junio a las 12:01 me escribiste:

http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html

Anyone want to do the honors and post to reddit, ycombinator, etc. ?


Nice article, but when I read:

T gcd(T)(T a, T b)
if (is(typeof(a % b)))
{
...
}

Under the presence of such beauty as template constraint syntax is,
"is(typeof(a % b))" makes my eyes hurt, and my brain wonder. When you
get used to this idiom, it might not look to bad, but I'm seeing people
new to D wonder "why in the hell was that syntax used?" while they try
to decode what is(typeof()) means.

I think is really a shame, all the beauty and clarity gained by the
simple "if" of template constraints, is lost with the cryptic
is(typeof()) (well, maybe not all, but a significant part).

I would love to see this fixed/simplified. For example, by applying the
changes in suggested in bug 3702 [1]. This looks much better, and is
pretty clear to anyone (even people that doesn't know D):

T gcd(T)(T a, T b)
if (meta.compiles(a % b))
{
...
}

I guess at this point this is not going to happen for D2, a real shame
:S

The, the article made me think that, even when template constraints are
useful not only for improving error reporting, they are often used only
for that. Since the compiler knows all the operations a template
parameter will need to satisfy from the function body, it would be nice
to have some sort of way to tell the compiler to write the template
constraints for us (the obvious ones at least, there might be other
template constraints desired besides the ones the ones the compiler can
figure out). This way, the errors can be improved without user
intervention.

[1] http://d.puremagic.com/issues/show_bug.cgi?id=3702




--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


Re: Constrained Templates

2010-06-13 Thread Leandro Lucarella
Walter Bright, el 13 de junio a las 12:01 me escribiste:
> http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html
> 
> Anyone want to do the honors and post to reddit, ycombinator, etc. ?

Nice article, but when I read:

T gcd(T)(T a, T b)
if (is(typeof(a % b)))
{
...
}

Under the presence of such beauty as template constraint syntax is,
"is(typeof(a % b))" makes my eyes hurt, and my brain wonder. When you
get used to this idiom, it might not look to bad, but I'm seeing people
new to D wonder "why in the hell was that syntax used?" while they try
to decode what is(typeof()) means.

I think is really a shame, all the beauty and clarity gained by the
simple "if" of template constraints, is lost with the cryptic
is(typeof()) (well, maybe not all, but a significant part).

I would love to see this fixed/simplified. For example, by applying the
changes in suggested in bug 3702 [1]. This looks much better, and is
pretty clear to anyone (even people that doesn't know D):

T gcd(T)(T a, T b)
if (meta.compiles(a % b))
{
...
}

I guess at this point this is not going to happen for D2, a real shame
:S

The, the article made me think that, even when template constraints are
useful not only for improving error reporting, they are often used only
for that. Since the compiler knows all the operations a template
parameter will need to satisfy from the function body, it would be nice
to have some sort of way to tell the compiler to write the template
constraints for us (the obvious ones at least, there might be other
template constraints desired besides the ones the ones the compiler can
figure out). This way, the errors can be improved without user
intervention.

[1] http://d.puremagic.com/issues/show_bug.cgi?id=3702

-- 
Leandro Lucarella (AKA luca) http://llucax.com.ar/
--
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
--
Salvajes, de traje, me quieren enseñar
Salvajes, de traje, me quieren educar


Re: Constrained Templates

2010-06-13 Thread Yao G.

http://www.reddit.com/r/programming/comments/cel4o/constrained_templates_in_the_d_programming/

On Sun, 13 Jun 2010 14:01:16 -0500, Walter Bright  
 wrote:



http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html

Anyone want to do the honors and post to reddit, ycombinator, etc. ?



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


Constrained Templates

2010-06-13 Thread Walter Bright

http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html

Anyone want to do the honors and post to reddit, ycombinator, etc. ?