www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Re: GoingNative 2012 to be livestreamed tomorrow - part 2

reply bearophile <bearophileHUGS lycos.com> writes:
Some more comments about the conference.

--------------------------

About "Variadic Templates are Funadic" by Andrei Alexandrescu fun talk:

I have had to see it at only 1-1.1X speed, to understand the language.

I can't see the laser spot in the video :-(

Thank you to Walter for designing D varidic templates in a simpler way. C++11
variadic templates look too much complex and over-engineered (example: the
lockstep expansion seems a bit crazy).

Slide 21: I didn't know that default is OK as first switch case too :-)

Even the questions&answers part of this talk was interesting enough.

------------------

About Bjarne Stroustrup and Andrew Sutton "A Concept Design for C++":

Regarding this code in Slide 12:

template<Number Num> Num gsqrt(Num);
gsqrt(2); // fine
gsqrt("Silly!"); // error: char* is not a Number


In D template constraints have two (or more) different usages:
1) To just remove a template from the pool of the usable ones;
2) In other situations only one template is present, and its constraints are a
way to give it some static typing. In this case I'd like better error messages.


Once this patch is applied:
https://github.com/D-Programming-Language/dmd/pull/692

you are able to write something like this, that isn't exceptionally nice
looking, but it's useful (it's going to make Phobos code a bit more hairy, but
the user is going to see some better error messages):


template IsNumberWithError(T, string file, int line) {
    enum bool IsNumberWithError = is( ...
    static if (!IsNumberWithError)
        __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a number.");
}

double gsqrt(T)(T x) if (IsNumberWithError!(T, __FILE__, __LINE__)) { /*...*/ }


An alternative is to give an else to the template constraints, but the error
message is at the bottom of the function, making it not easy to find, so I
don't like this syntax:


int spam(T)(T x) if (IsFoo!T || IsBar!T) {
    // ...
} else {
    __ctfeWriteln("'", typeid(T), "' is not Foo or Bar.");
}


If Concepts are a type system for templates, then "Current template code
remains valid. Constrained and "traditional" templates must interoperate" means
"gradual typing", or "optional typing" (see recent Racket Scheme).


Slide 37, "Concepts for the STL (N3351)": how many of them are already in
Phobos? Are the missing ones needed/useful for Phobos?


This is Slide 39 and 40 are quite nice:

template<InputIterator Iter, Predicate<ValueType<Iter>> Pred>
bool all_of(Iter first, Iter last, Pred pred);

std::find_if():
template<InputIterator Iter, Predicate<ValueType<Iter>> Pred>
Iter find_if(Iter first, Iter last, Pred pred);


Template aliases seem nice, not to save a bit of code to avoid defining another
template, but to allow deducibility as explained here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1449.pdf

------------------

In the "Panel: Ask Us Anything!" why didn't Andrei comment about the experience
of compile-time function execution of D :-)


Later I'll discuss some bits from the talk "Clang - Defending C++ from Murphy's
Million Monkeys" in the main D newsgroup because it contains some things more
directly relevant for D and its compiler.

Bye,
bearophile
Feb 09 2012
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 2/10/12, bearophile <bearophileHUGS lycos.com> wrote:
 In the "Panel: Ask Us Anything!" why didn't Andrei comment about the
 experience of compile-time function execution of D :-)

I think he didn't want to go off-topic. Maybe he discusses D in private chats with Herb & Co., but since this was a C++ Q&A it would probably be rude to talk about other languages.
Feb 09 2012
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-02-10 02:47, bearophile wrote:
 Some more comments about the conference.

 --------------------------

 About "Variadic Templates are Funadic" by Andrei Alexandrescu fun talk:

 I have had to see it at only 1-1.1X speed, to understand the language.

 I can't see the laser spot in the video :-(

 Thank you to Walter for designing D varidic templates in a simpler way. C++11
variadic templates look too much complex and over-engineered (example: the
lockstep expansion seems a bit crazy).

 Slide 21: I didn't know that default is OK as first switch case too :-)

 Even the questions&answers part of this talk was interesting enough.

 ------------------

 About Bjarne Stroustrup and Andrew Sutton "A Concept Design for C++":

 Regarding this code in Slide 12:

 template<Number Num>  Num gsqrt(Num);
 gsqrt(2); // fine
 gsqrt("Silly!"); // error: char* is not a Number


 In D template constraints have two (or more) different usages:
 1) To just remove a template from the pool of the usable ones;
 2) In other situations only one template is present, and its constraints are a
way to give it some static typing. In this case I'd like better error messages.


 Once this patch is applied:
 https://github.com/D-Programming-Language/dmd/pull/692

 you are able to write something like this, that isn't exceptionally nice
looking, but it's useful (it's going to make Phobos code a bit more hairy, but
the user is going to see some better error messages):


 template IsNumberWithError(T, string file, int line) {
      enum bool IsNumberWithError = is( ...
      static if (!IsNumberWithError)
          __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a
number.");
 }

 double gsqrt(T)(T x) if (IsNumberWithError!(T, __FILE__, __LINE__)) { /*...*/ }

Wouldn't this be possible: template IsNumberWithError(T, string file = __FILE__, int line = __LINE__) { enum bool IsNumberWithError = is( ... static if (!IsNumberWithError) __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a number."); } double gsqrt(T)(T x) if (IsNumberWithError!(T)) { /*...*/ } __FILE__ and __LINE__ would be picked up from the "calling" point and not the declaration point of IsNumberWithError. We already have this in some cases. -- /Jacob Carlborg
Feb 09 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 02/10/2012 08:17 AM, Jacob Carlborg wrote:
 On 2012-02-10 02:47, bearophile wrote:
 Some more comments about the conference.

 --------------------------

 About "Variadic Templates are Funadic" by Andrei Alexandrescu fun talk:

 I have had to see it at only 1-1.1X speed, to understand the language.

 I can't see the laser spot in the video :-(

 Thank you to Walter for designing D varidic templates in a simpler
 way. C++11 variadic templates look too much complex and
 over-engineered (example: the lockstep expansion seems a bit crazy).

 Slide 21: I didn't know that default is OK as first switch case too :-)

 Even the questions&answers part of this talk was interesting enough.

 ------------------

 About Bjarne Stroustrup and Andrew Sutton "A Concept Design for C++":

 Regarding this code in Slide 12:

 template<Number Num> Num gsqrt(Num);
 gsqrt(2); // fine
 gsqrt("Silly!"); // error: char* is not a Number


 In D template constraints have two (or more) different usages:
 1) To just remove a template from the pool of the usable ones;
 2) In other situations only one template is present, and its
 constraints are a way to give it some static typing. In this case I'd
 like better error messages.


 Once this patch is applied:
 https://github.com/D-Programming-Language/dmd/pull/692

 you are able to write something like this, that isn't exceptionally
 nice looking, but it's useful (it's going to make Phobos code a bit
 more hairy, but the user is going to see some better error messages):


 template IsNumberWithError(T, string file, int line) {
 enum bool IsNumberWithError = is( ...
 static if (!IsNumberWithError)
 __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a number.");
 }

 double gsqrt(T)(T x) if (IsNumberWithError!(T, __FILE__, __LINE__)) {
 /*...*/ }

Wouldn't this be possible: template IsNumberWithError(T, string file = __FILE__, int line = __LINE__) { enum bool IsNumberWithError = is( ... static if (!IsNumberWithError) __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a number."); } double gsqrt(T)(T x) if (IsNumberWithError!(T)) { /*...*/ } __FILE__ and __LINE__ would be picked up from the "calling" point and not the declaration point of IsNumberWithError. We already have this in some cases.

Unfortunately, they would point to the template constraint. But I think it would be a quite useful enhancement to pick up __FILE__ and __LINE__ at template instantiation point for the template constraint.
Feb 10 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-02-10 13:58, Timon Gehr wrote:
 On 02/10/2012 08:17 AM, Jacob Carlborg wrote:
 On 2012-02-10 02:47, bearophile wrote:
 Some more comments about the conference.

 --------------------------

 About "Variadic Templates are Funadic" by Andrei Alexandrescu fun talk:

 I have had to see it at only 1-1.1X speed, to understand the language.

 I can't see the laser spot in the video :-(

 Thank you to Walter for designing D varidic templates in a simpler
 way. C++11 variadic templates look too much complex and
 over-engineered (example: the lockstep expansion seems a bit crazy).

 Slide 21: I didn't know that default is OK as first switch case too :-)

 Even the questions&answers part of this talk was interesting enough.

 ------------------

 About Bjarne Stroustrup and Andrew Sutton "A Concept Design for C++":

 Regarding this code in Slide 12:

 template<Number Num> Num gsqrt(Num);
 gsqrt(2); // fine
 gsqrt("Silly!"); // error: char* is not a Number


 In D template constraints have two (or more) different usages:
 1) To just remove a template from the pool of the usable ones;
 2) In other situations only one template is present, and its
 constraints are a way to give it some static typing. In this case I'd
 like better error messages.


 Once this patch is applied:
 https://github.com/D-Programming-Language/dmd/pull/692

 you are able to write something like this, that isn't exceptionally
 nice looking, but it's useful (it's going to make Phobos code a bit
 more hairy, but the user is going to see some better error messages):


 template IsNumberWithError(T, string file, int line) {
 enum bool IsNumberWithError = is( ...
 static if (!IsNumberWithError)
 __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a number.");
 }

 double gsqrt(T)(T x) if (IsNumberWithError!(T, __FILE__, __LINE__)) {
 /*...*/ }

Wouldn't this be possible: template IsNumberWithError(T, string file = __FILE__, int line = __LINE__) { enum bool IsNumberWithError = is( ... static if (!IsNumberWithError) __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a number."); } double gsqrt(T)(T x) if (IsNumberWithError!(T)) { /*...*/ } __FILE__ and __LINE__ would be picked up from the "calling" point and not the declaration point of IsNumberWithError. We already have this in some cases.

Unfortunately, they would point to the template constraint. But I think it would be a quite useful enhancement to pick up __FILE__ and __LINE__ at template instantiation point for the template constraint.

We already have this for regular templates so why wouldn't it work. It's documented here: http://www.dlang.org/template.html Search for "Template Value Parameters". -- /Jacob Carlborg
Feb 10 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/10/2012 02:44 PM, Jacob Carlborg wrote:
 On 2012-02-10 13:58, Timon Gehr wrote:
 On 02/10/2012 08:17 AM, Jacob Carlborg wrote:
 On 2012-02-10 02:47, bearophile wrote:
 Some more comments about the conference.

 --------------------------

 About "Variadic Templates are Funadic" by Andrei Alexandrescu fun talk:

 I have had to see it at only 1-1.1X speed, to understand the language.

 I can't see the laser spot in the video :-(

 Thank you to Walter for designing D varidic templates in a simpler
 way. C++11 variadic templates look too much complex and
 over-engineered (example: the lockstep expansion seems a bit crazy).

 Slide 21: I didn't know that default is OK as first switch case too :-)

 Even the questions&answers part of this talk was interesting enough.

 ------------------

 About Bjarne Stroustrup and Andrew Sutton "A Concept Design for C++":

 Regarding this code in Slide 12:

 template<Number Num> Num gsqrt(Num);
 gsqrt(2); // fine
 gsqrt("Silly!"); // error: char* is not a Number


 In D template constraints have two (or more) different usages:
 1) To just remove a template from the pool of the usable ones;
 2) In other situations only one template is present, and its
 constraints are a way to give it some static typing. In this case I'd
 like better error messages.


 Once this patch is applied:
 https://github.com/D-Programming-Language/dmd/pull/692

 you are able to write something like this, that isn't exceptionally
 nice looking, but it's useful (it's going to make Phobos code a bit
 more hairy, but the user is going to see some better error messages):


 template IsNumberWithError(T, string file, int line) {
 enum bool IsNumberWithError = is( ...
 static if (!IsNumberWithError)
 __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a
 number.");
 }

 double gsqrt(T)(T x) if (IsNumberWithError!(T, __FILE__, __LINE__)) {
 /*...*/ }

Wouldn't this be possible: template IsNumberWithError(T, string file = __FILE__, int line = __LINE__) { enum bool IsNumberWithError = is( ... static if (!IsNumberWithError) __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a number."); } double gsqrt(T)(T x) if (IsNumberWithError!(T)) { /*...*/ } __FILE__ and __LINE__ would be picked up from the "calling" point and not the declaration point of IsNumberWithError. We already have this in some cases.

Unfortunately, they would point to the template constraint. But I think it would be a quite useful enhancement to pick up __FILE__ and __LINE__ at template instantiation point for the template constraint.

We already have this for regular templates so why wouldn't it work. It's documented here: http://www.dlang.org/template.html Search for "Template Value Parameters".

I know the spec by heart :P. But you are right, your example does the same thing as bearophile's. I did not read his post carefully enough, I assumed he wanted the error message point to instantiation side of the constrained template.
Feb 10 2012
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 02/10/2012 02:47 AM, bearophile wrote:
 Once this patch is applied:
 https://github.com/D-Programming-Language/dmd/pull/692

 you are able to write something like this, that isn't exceptionally nice
looking, but it's useful (it's going to make Phobos code a bit more hairy, but
the user is going to see some better error messages):


 template IsNumberWithError(T, string file, int line) {
      enum bool IsNumberWithError = is( ...
      static if (!IsNumberWithError)
          __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a
number.");
 }

 double gsqrt(T)(T x) if (IsNumberWithError!(T, __FILE__, __LINE__)) { /*...*/ }

__ctfeWriteln is not required (it is not a declaration, therefore it probably won't work). pragma(msg, ...) can already be used for that purpose. template IsNumberWithError(T, string file, int line) { enum bool IsNumberWithError = is( ... static if (!IsNumberWithError) pragma(msg,file, "(", line, "): '", T, "' is not a number."); }
 An alternative is to give an else to the template constraints, but the error
message is at the bottom of the function, making it not easy to find, so I
don't like this syntax:


 int spam(T)(T x) if (IsFoo!T || IsBar!T) {
      // ...
 } else {
      __ctfeWriteln("'", typeid(T), "' is not Foo or Bar.");
 }

I like it. The else clause could perform arbitrary checks to make the error message as helpful as possible.
Feb 10 2012
next sibling parent reply Artur Skawina <art.08.09 gmail.com> writes:
On 02/10/12 14:04, Timon Gehr wrote:
 On 02/10/2012 02:47 AM, bearophile wrote:
 An alternative is to give an else to the template constraints, but the error
message is at the bottom of the function, making it not easy to find, so I
don't like this syntax:


 int spam(T)(T x) if (IsFoo!T || IsBar!T) {
      // ...
 } else {
      __ctfeWriteln("'", typeid(T), "' is not Foo or Bar.");
 }

I like it. The else clause could perform arbitrary checks to make the error message as helpful as possible.

I can see it work for the single template case (i've used ctfe'd functions just for the error messages like that), but what if there are several spam()s with different constraints? The else clauses only get run when no match is found? artur
Feb 10 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/10/2012 02:21 PM, Artur Skawina wrote:
 On 02/10/12 14:04, Timon Gehr wrote:
 On 02/10/2012 02:47 AM, bearophile wrote:
 An alternative is to give an else to the template constraints, but the error
message is at the bottom of the function, making it not easy to find, so I
don't like this syntax:


 int spam(T)(T x) if (IsFoo!T || IsBar!T) {
       // ...
 } else {
       __ctfeWriteln("'", typeid(T), "' is not Foo or Bar.");
 }

I like it. The else clause could perform arbitrary checks to make the error message as helpful as possible.

I can see it work for the single template case (i've used ctfe'd functions just for the error messages like that), but what if there are several spam()s with different constraints? The else clauses only get run when no match is found? artur

That is how I assumed it would work.
Feb 10 2012
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Timon Gehr:

 __ctfeWriteln is not required (it is not a declaration, therefore it 
 probably won't work).

Then IsNumberWithError needs to be compile-time function.
 pragma(msg, ...) can already be used for that purpose.

I don't like pragma(msg), it even forces a newline at the end. Bye, bearophile
Feb 10 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 02/11/2012 12:27 AM, bearophile wrote:
 Timon Gehr:

 __ctfeWriteln is not required (it is not a declaration, therefore it
 probably won't work).

Then IsNumberWithError needs to be compile-time function.

I'm not sure what you are saying here.
 pragma(msg, ...) can already be used for that purpose.

I don't like pragma(msg), it even forces a newline at the end.

So does __ctfeWriteln.
 Bye,
 bearophile

Feb 10 2012
parent reply bearophile <bearophileHUGS lycos.com> writes:
Timon Gehr:

 I'm not sure what you are saying here.

I meant using something like this, instead of a template: bool IsNumberWithError(T, string file=__FILE__, int line=__LINE__)() { enum bool result = is( ... if (!result) __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a number."); return result; }
 So does __ctfeWriteln.

I know, but I hope __ctfeWriteln will become __ctfeWrite or someone will write a __ctfeWrite too. See the pull request for discussions about this. Having no way to avoid the ending newline is awful, it forces me to build large strings with appends and print them all at once instead of using just a series or writes. Not having an ending newline was my first requirement for an acceptable compile-time printing function. Bye, bearophile
Feb 10 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/11/2012 01:47 AM, bearophile wrote:
 Timon Gehr:

 I'm not sure what you are saying here.

I meant using something like this, instead of a template: bool IsNumberWithError(T, string file=__FILE__, int line=__LINE__)() { enum bool result = is( ... if (!result) __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a number."); return result; }

I consider pragma msg a superior solution for this specific case.
 So does __ctfeWriteln.

I know, but I hope __ctfeWriteln will become __ctfeWrite or someone will write a __ctfeWrite too. See the pull request for discussions about this. Having no way to avoid the ending newline is awful, it forces me to build large strings with appends and print them all at once instead of using just a series or writes. Not having an ending newline was my first requirement for an acceptable compile-time printing function. Bye, bearophile

I agree and don't think there are any valid counter-arguments.
Feb 10 2012