www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proposal: Static Invariant (C++ concept in D)

reply davidl <davidl 126.com> writes:
I've read a little bit stuff of C++ concept, and find it has some  
similarity with
D's runtime invariant in some abstraction. In some extent , we can imagine  
C++
concept is kind of compile time invariant.

So the following:

class TemplateClass(T)
{
   static invariant()   // reuse keywords, nothing more get introduced
   {
     // prerequisite checking, does T have foo , does T have bar
     Base_Concept_check();  // these emulate the inheritance of concept
     Derived_Concept_check();
   }
}

Thanks for the great discussion & help from h3r3tic, LeoD, Alexander,  
Larsivi
They gave me some comments, and ideas.

Though it might not functionate all possiblities of C++ concept , but it  
brings
us a little bit more powerful way to make sure our templates go all fine.  
Hope
people love this idea :D


DavidL
Sep 17 2007
next sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
davidl wrote:
 Thanks for the great discussion & help from h3r3tic, LeoD, Alexander, 
 Larsivi
 They gave me some comments, and ideas.
*cough* negative comments *cough* -- Tomasz Stachowiak http://h3.team0xf.com/ h3/h3r3tic on #D freenode
Sep 17 2007
parent reply Alexander Panek <alexander.panek brainsware.org> writes:
Tom S wrote:
 davidl wrote:
 Thanks for the great discussion & help from h3r3tic, LeoD, Alexander, 
 Larsivi
 They gave me some comments, and ideas.
*cough* negative comments *cough*
Same here; read as: I don't like it either.
Sep 17 2007
parent reply davidl <davidl 126.com> writes:
在 Mon, 17 Sep 2007 20:45:07 +0800,Alexander Panek  
<alexander.panek brainsware.org> 写道:

 Tom S wrote:
 davidl wrote:
 Thanks for the great discussion & help from h3r3tic, LeoD, Alexander,  
 Larsivi
 They gave me some comments, and ideas.
*cough* negative comments *cough*
Same here; read as: I don't like it either.
You didn't raise a valid point. And actually it's you who choose the name. -- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
Sep 17 2007
parent reply Alexander Panek <alexander.panek brainsware.org> writes:
davidl wrote:
 You didn't raise a valid point. And actually it's you who choose the name.
I pointed out - as Bill Baxter did here in the newsgroup -, that your proposal is rendered useless. "static in" would only add syntactic sugar for a feature that is rather easy to implement in D already. I pointed out the name, because I thought /if/ there should be a kind of compile time constraint, "static variadic" would be the most fitting phrase - as opposed to "static in", which you kept repeating in the beginning of our "discussion".
Sep 17 2007
parent reply Alexander Panek <alexander.panek brainsware.org> writes:
Alexander Panek wrote:
 [...] compile time constraint, "static variadic" would be the most fitting
[...]
That should be "static invariant" of course ... !
Sep 17 2007
parent davidl <davidl 126.com> writes:
在 Mon, 17 Sep 2007 22:15:11 +0800,Alexander Panek  
<alexander.panek brainsware.org> 写道:

 Alexander Panek wrote:
 [...] compile time constraint, "static variadic" would be the most  
 fitting [...]
That should be "static invariant" of course ... !
I replied to bb, and I don't know if that answer good enough for you. -- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
Sep 17 2007
prev sibling next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
davidl wrote:
 I've read a little bit stuff of C++ concept, and find it has some 
 similarity with
 D's runtime invariant in some abstraction. In some extent , we can 
 imagine C++
 concept is kind of compile time invariant.
 
 So the following:
 
 class TemplateClass(T)
 {
   static invariant()   // reuse keywords, nothing more get introduced
   {
     // prerequisite checking, does T have foo , does T have bar
     Base_Concept_check();  // these emulate the inheritance of concept
     Derived_Concept_check();
   }
 }
Can't you just do the same thing now, with slightly different syntax? For instance Base_Concept_check could be a template mixin with nothing but static ifs and other compile-time checks. Then you just have to say "mixin Base_Concept_check;" instead of calling it like a function, but same basic thing, no? --bb
Sep 17 2007
parent reply davidl <davidl 126.com> writes:
在 Mon, 17 Sep 2007 20:55:12 +0800,Bill Baxter  
<dnewsgroup billbaxter.com> 写道:

 davidl wrote:
 I've read a little bit stuff of C++ concept, and find it has some  
 similarity with
 D's runtime invariant in some abstraction. In some extent , we can  
 imagine C++
 concept is kind of compile time invariant.
  So the following:
  class TemplateClass(T)
 {
   static invariant()   // reuse keywords, nothing more get introduced
   {
     // prerequisite checking, does T have foo , does T have bar
     Base_Concept_check();  // these emulate the inheritance of concept
     Derived_Concept_check();
   }
 }
Can't you just do the same thing now, with slightly different syntax? For instance Base_Concept_check could be a template mixin with nothing but static ifs and other compile-time checks. Then you just have to say "mixin Base_Concept_check;" instead of calling it like a function, but same basic thing, no? --bb
it's a little bit different. Because the check means the prerequisite of the template, This is for the whole template scope. not till the last moment of making a call. So a mixin would trigger the "static invariant". And "static invariant" itself is for prerequisite checking , so it's an enforcement for template design. The template inner scope can only use the stuffs which are checked in the "static invariant", I mean any stuff or members related to type T. Also IDE could provide nice code completion for template writers. I think it's for elegance. With some prerequisite checking the template system would be nicer. -- 使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
Sep 17 2007
parent Alexander Panek <alexander.panek brainsware.org> writes:
davidl wrote:
 [...] 
 I think it's for elegance. With some prerequisite checking the template 
 system would be
 nicer.
I think this is one of the cases where a feature should be implemented in a library, rather than the language specification.
Sep 17 2007
prev sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
davidl Wrote:

 I've read a little bit stuff of C++ concept, and find it has some  
 similarity with
 D's runtime invariant in some abstraction. In some extent , we can imagine  
 C++
 concept is kind of compile time invariant.
 
 So the following:
 
 class TemplateClass(T)
 {
    static invariant()   // reuse keywords, nothing more get introduced
    {
      // prerequisite checking, does T have foo , does T have bar
      Base_Concept_check();  // these emulate the inheritance of concept
      Derived_Concept_check();
    }
 }
 
 Thanks for the great discussion & help from h3r3tic, LeoD, Alexander,  
 Larsivi
 They gave me some comments, and ideas.
 
 Though it might not functionate all possiblities of C++ concept , but it  
 brings
 us a little bit more powerful way to make sure our templates go all fine.  
 Hope
 people love this idea :D
 
 
 DavidL
Isn't this what a static assert is? If you want sets of static assertions, you can always use template mixins. template LionAssertions() { static assert(Lion.foodChainPosition == FoodChain.max); static assert(Mufasa > Aslan, "this program contains evil lies"); } class Lion : AwesomenessIncarnate { mixin LionAssertions!() }
Sep 17 2007