Allowing arbitrary types for a function's argument and return type

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 22 08:58:28 PDT 2015


On Thursday, 22 October 2015 at 15:10:58 UTC, pineapple wrote:
> On Thursday, 22 October 2015 at 14:36:52 UTC, John Colvin wrote:
>> Using ranges instead of threads or fibers, slightly 
>> over-engineered to show off features:
>
> What does if(isIntegral!T) do? It looks like it would verify 
> that the template type is a discrete number? If I were to 
> create my own class, say a BigNum as an example, how could I 
> specify that the isIntegral condition should be met for it?

Only with builtin types: 
http://dlang.org/phobos/std_traits.html#isIntegral
However there are a variety of ways you could mark special 
properties of types and have your own isIntegral-like template 
that recognised them. user-defined-attributes are one 
possibility, adding an `enum isIntegral = true;` is another.

> Apart from the aesthetics, what are the functional differences 
> between using recurrence and using a Generator? Will one be 
> more efficient than the other?

ranges are likely to be more efficient because, apart from 
anything else, they are easier for the compiler to reason about 
and optimise. Both are good tools, but ranges are more widespread 
in D.

> It's not fair how easy it is to incorporate unit tests in D. 
> Now what excuse will I have when my code is buggy?

:)


More information about the Digitalmars-d-learn mailing list