Use UFCS for reducing dependencies

Paul Backus snarwin at gmail.com
Mon Jul 18 12:46:20 UTC 2022


On Monday, 18 July 2022 at 10:40:23 UTC, Hipreme wrote:
> On Sunday, 17 July 2022 at 16:15:07 UTC, Paul Backus wrote:
>>
>> Another possibility:
>>
>> ```d
>> struct PNG
>> {
>>     static PNG load(Source)(Source source)
>>         if (isInputRange!Source && is(ElementType!Source == 
>> ubyte))
>>     {
>>         // etc.
>>     }
>> }
>> ```
>>
>> This way, the PNG module itself is completely agnostic about 
>> what data source it loads from, and has no explicit 
>> dependencies (except on `std.range`, I guess).
>
>
> Could you extend it a bit further how would that work?

Well, I don't know the algorithm for PNG decoding, so I don't 
know whether it requires an input range, a forward range, or a 
random access range. But the basic idea is, you declare your 
`load` function as taking a generic range type, and then you can 
load from anything that implements the appropriate range 
interface.

Because the algorithm (`PNG.load`) and the data sources 
communicate only via the range API, neither one needs to have any 
special knowledge of, or explicit dependency on, the other.

This is the fundamental idea behind ranges. If you have N data 
sources that implement the range interface, and M algorithms that 
consume the range interface, then you do not need to implement 
all M×N combinations by hand--all of your data sources and all of 
your algorithms will Just Work™ with each other.


More information about the Digitalmars-d mailing list