[OT] What are D's values?
Paul Backus
snarwin at gmail.com
Tue Oct 5 00:11:29 UTC 2021
On Monday, 4 October 2021 at 23:10:27 UTC, Tejas wrote:
> Can you please list any resources on DbI?
>
> I've been finding it pretty hard to grasp, and I only managed
> to find a few videos by Andrei on the subject.
>
> Even the d-idioms website doesn't have much.
>
> Any text sources would be really appreciated.
>
> Thank you for reading!
Design by introspection is when you have templated code
"customize" itself based on the specific type(s) it's
instantiated with.
One place DbI is used a lot is in `std.range`. For example,
here's how `std.range`'s documentation describes the return value
of `retro`:
> Type: auto
>
> A bidirectional range with length if r also provides a length.
> Or, if r is a random access range, then the return value will
> be random access as well.
Source: https://phobos.dpldocs.info/std.range.retro.html#returns
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.
More information about the Digitalmars-d
mailing list