www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Static inheritance (proof of concept)

reply Iakh <iaktakh gmail.com> writes:
There was discussion and proposal to extend D with static 
inheritance:
http://forum.dlang.org/thread/jwzxngccuwwizyivpeaf forum.dlang.org

But it could be done just with mixins, UDAs and some rewriting of
predicates.

Exaple:

 concept!isInputRange()
struct Range
{
     mixin checkConcepts;

     int front()
     {
         return 0;
     }
}

concept-check ~master: building configuration "application"...
source/app.d(73,0): Error: can't test for empty
source/app.d(73,0): Error: can't invoke popFront()
source/app.d(67,5): Error: static assert  "Range is not a 
concept!(isInputRange)"

checker function prototype:
     bool checkConcept(Flag!"verbose" verbose = No.verbose,
             string file = "", size_t line = "")()

if verbose==false this function could be used in template 
constraint
just to check is template parameter match conditions without 
printing errors

full code:
http://dpaste.dzfl.pl/5056ad68c7b4
Feb 27 2016
next sibling parent reply Chris Wright <dhasenan gmail.com> writes:
On Sat, 27 Feb 2016 13:35:30 +0000, Iakh wrote:

 There was discussion and proposal to extend D with static inheritance:
 http://forum.dlang.org/thread/jwzxngccuwwizyivpeaf forum.dlang.org
 
 But it could be done just with mixins, UDAs and some rewriting of
 predicates.
 
 Exaple:
 
  concept!isInputRange()
 struct Range {
      mixin checkConcepts;
 
      int front()
      {
          return 0;
      }
 }
Looks good. I'd prefer to have just the mixin or just the attribute -- the latter being tricky just now. It'd be just as easy to make it: struct Range { mixin ensureConcept!isInputRange; }
Feb 27 2016
parent reply Iakh <iaktakh gmail.com> writes:
On Saturday, 27 February 2016 at 18:14:25 UTC, Chris Wright wrote:
 On Sat, 27 Feb 2016 13:35:30 +0000, Iakh wrote:

 Looks good. I'd prefer to have just the mixin or just the 
 attribute -- the latter being tricky just now.
Agree.
 It'd be just as easy to make it:

 struct Range {
   mixin ensureConcept!isInputRange;
 }
It is hard to pass all params to the mixin such as file, line and additional params for concept: concept!(isOutputRange, int)() // isSomething!(T, a, b, c, d, ...) struct Range { mixin checkConcepts; // mixin takes current line and file with default params to do error checking so there is no way to use variadic args (is it?) }
Feb 27 2016
parent reply Chris Wright <dhasenan gmail.com> writes:
On Sat, 27 Feb 2016 19:45:58 +0000, Iakh wrote:

 It is hard to pass all params to the mixin such as file, line and
 additional params for concept:
Maybe we need file and line number __traits.
Feb 27 2016
parent Iakh <iaktakh gmail.com> writes:
On Saturday, 27 February 2016 at 20:51:22 UTC, Chris Wright wrote:
 On Sat, 27 Feb 2016 19:45:58 +0000, Iakh wrote:

 It is hard to pass all params to the mixin such as file, line 
 and additional params for concept:
Maybe we need file and line number __traits.
Nice idea. I am also voting for function formatCompilerErrorMessage(text, file, line);
Feb 27 2016
prev sibling parent reply Atila Neves <atila.neves gmail.com> writes:
On Saturday, 27 February 2016 at 13:35:30 UTC, Iakh wrote:
 There was discussion and proposal to extend D with static 
 inheritance:
 http://forum.dlang.org/thread/jwzxngccuwwizyivpeaf forum.dlang.org

 But it could be done just with mixins, UDAs and some rewriting 
 of
 predicates.
It can, yes: https://github.com/D-Programming-Language/phobos/pull/3677 That went nowhere, hence the DIP. Atila
Feb 29 2016
parent reply Iakh <iaktakh gmail.com> writes:
On Monday, 29 February 2016 at 11:13:18 UTC, Atila Neves wrote:
 On Saturday, 27 February 2016 at 13:35:30 UTC, Iakh wrote:
 There was discussion and proposal to extend D with static 
 inheritance:
 http://forum.dlang.org/thread/jwzxngccuwwizyivpeaf forum.dlang.org

 But it could be done just with mixins, UDAs and some rewriting 
 of
 predicates.
It can, yes: https://github.com/D-Programming-Language/phobos/pull/3677 That went nowhere, hence the DIP. Atila
I'm not familiar with github so i can't see why it was rejected besides auto testing tools (Looks like it's just outdated)
Feb 29 2016
parent reply Atila Neves <atila.neves gmail.com> writes:
On Monday, 29 February 2016 at 12:31:58 UTC, Iakh wrote:
 On Monday, 29 February 2016 at 11:13:18 UTC, Atila Neves wrote:
 On Saturday, 27 February 2016 at 13:35:30 UTC, Iakh wrote:
 There was discussion and proposal to extend D with static 
 inheritance:
 http://forum.dlang.org/thread/jwzxngccuwwizyivpeaf forum.dlang.org

 But it could be done just with mixins, UDAs and some 
 rewriting of
 predicates.
It can, yes: https://github.com/D-Programming-Language/phobos/pull/3677 That went nowhere, hence the DIP. Atila
I'm not familiar with github so i can't see why it was rejected besides auto testing tools (Looks like it's just outdated)
http://forum.dlang.org/post/tgnxocozkurfvmxqofnn forum.dlang.org Atila
Feb 29 2016
next sibling parent Iakh <iaktakh gmail.com> writes:
On Monday, 29 February 2016 at 13:31:11 UTC, Atila Neves wrote:
 I'm not familiar with github so i can't see why it was rejected
 besides auto testing tools (Looks like it's just outdated)
http://forum.dlang.org/post/tgnxocozkurfvmxqofnn forum.dlang.org Atila
I didn't get the point. But I could propose one more way to implement it:) Just with template mixins. If concept takes just one template argument (as isInputRange do) then it could be passed just by name, but if there are several args you have to fully instantiate it: struct Range { mixin checkConcept!isInputRange; mixin checkConcept!(isOutputRange!(Range, int)); int front() { return 0; } } This way template mixin could take source line and file to generate better report. First template arg of isOutputRange could be checked is it the same as wrapping type to prevent copy-paste mistakes.
Feb 29 2016
prev sibling parent Iakh <iaktakh gmail.com> writes:
On Monday, 29 February 2016 at 13:31:11 UTC, Atila Neves wrote:
 http://forum.dlang.org/post/tgnxocozkurfvmxqofnn forum.dlang.org

 Atila
If you about this: http://forum.dlang.org/post/eejiauievypbfifkyvvo forum.dlang.org
On Wednesday, 29 July 2015 at 06:05:37 UTC, Kagamin wrote:
 On Tuesday, 28 July 2015 at 13:10:43 UTC, Atila Neves wrote:
 I guess, but not easily. I've written template mixins to do 
 that before and it was awkward.
What was awkward?
Writing a generic solution that would work for multiple constraints without code repetition. Atila
It could be done without type name repetition: mixin checkConcept!isInputRange; mixin checkConcept!(isOutputRange!(PlaceHolderForThisT, int)); Last one is even more general. It could handle checkers with odd order of parameters.
Mar 01 2016