www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Partial return type specification

reply bearophile <bearophileHUGS lycos.com> writes:
Pelle:

 string assertReturnsRangeOf(T)() {
      return "static assert (is(Unqual!(ForeachType!(typeof(return))) == "
          ~ T.stringof ~ "));";
 }
 
 auto foo() {
      return map!q{a*a}([1,2,3,4,5]);
      mixin (assertReturnsRangeOf!int);
 }
 
 You were probably looking for a more general and, well, good, solution. 
 This does however convey some intent, and kind of works.

Ideally the partial specification syntax for ranges may work at the calling point too: void main() { Range!int results = foo(); } Here results is of its specific type, it's not a "Range!int", so Range!int works as "auto". The difference is that the compiler makes sure that's an iterable of ints, and not an iterable of floats or a not iterable, etc. Bye, bearophile
Oct 11 2010
parent reply Pelle <pelle.mansson gmail.com> writes:
On 10/11/2010 10:56 PM, bearophile wrote:
 Pelle:

 string assertReturnsRangeOf(T)() {
       return "static assert (is(Unqual!(ForeachType!(typeof(return))) == "
           ~ T.stringof ~ "));";
 }

 auto foo() {
       return map!q{a*a}([1,2,3,4,5]);
       mixin (assertReturnsRangeOf!int);
 }

 You were probably looking for a more general and, well, good, solution.
 This does however convey some intent, and kind of works.

Ideally the partial specification syntax for ranges may work at the calling point too: void main() { Range!int results = foo(); } Here results is of its specific type, it's not a "Range!int", so Range!int works as "auto". The difference is that the compiler makes sure that's an iterable of ints, and not an iterable of floats or a not iterable, etc. Bye, bearophile

This I would very much like. Also, being able to specify that you return a Range!int or ForwardRange!int would be very useful. Without losing the value types, of course. :-)
Oct 11 2010
parent Peter Alexander <peter.alexander.au gmail.com> writes:
On 12/10/10 7:28 AM, Pelle wrote:
 On 10/11/2010 10:56 PM, bearophile wrote:
 Ideally the partial specification syntax for ranges may work at the
 calling point too:

 void main() {
 Range!int results = foo();
 }

 Here results is of its specific type, it's not a "Range!int", so
 Range!int works as "auto". The difference is that the compiler makes
 sure that's an iterable of ints, and not an iterable of floats or a
 not iterable, etc.

 Bye,
 bearophile

This I would very much like. Also, being able to specify that you return a Range!int or ForwardRange!int would be very useful. Without losing the value types, of course. :-)

So basically these 'partial types' are like implicit static-polymorphism interfaces? i.e. they make guarantees about the interface, but don't require that the types explicitly derive from them? Presumably you could use these to specify template constraints as well? e.g. int gcd(InputRange!int Range)(Range r) { ... } which would be equivalent to: int gcd(Range)(Range r) if (isInputRange!Range) { ... } Seems like the logical thing to do.
Oct 12 2010