[OT] What are D's values?

Paul Backus snarwin at gmail.com
Tue Oct 5 01:33:45 UTC 2021

On Tuesday, 5 October 2021 at 00:51:26 UTC, Tejas wrote:
> On Tuesday, 5 October 2021 at 00:11:29 UTC, Paul Backus wrote:
>> Notice those "if" clauses. The return value has a `.length` 
>> property **if** the original range, `r`, has a `.length` 
>> property. That's DbI.
>> How is it done? Internally, `retro` uses introspection 
>> features like `__traits` and `is()` expressions to check what 
>> properties `r` has, and uses `static if` to customize its own 
>> implementation based on the result of those checks.
> Is that seriously all there is to it? I see Andrei saying in 
> his talks that `static if` doubles the design space covered, 
> that DbI can tackle combinatorial explosion.
> Can DbI really help with software that gets complex 
> exponentially?

Whenever you write a `static if` statement, you end up with two 
possible versions of the code: one where the condition is true, 
and one where it's false. It follows that if you have N `static 
if` statements in your program, then you have 2^N possible 
versions of the code--so the number of versions increases 
exponentially with respect to the number of `static if` 

If you assume that "number of `static if` statements" is 
proportional to "number of lines of code", then you can make the 
claim that DbI allows you to write programs in O(N) lines that 
would otherwise take O(2^N) lines.

Of course, this is not really true in practice. In languages that 
don't support DbI, what actually happens is that you do not write 
all 2^N customized versions of the code. Instead, you give up on 
having individual customized versions for each use-case and write 
a single version based on some lowest-common-denominator 
abstraction. What you really lose here are the benefits to 
performance and expressiveness that come from having 
individually-customized versions of your code.

More information about the Digitalmars-d mailing list