Article: Writing Julia style multiple dispatch code in D

jmh530 via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Thu Aug 24 17:35:24 PDT 2017


On Thursday, 24 August 2017 at 23:50:21 UTC, data pulverizer 
wrote:
>
> ```
> double density(D: UnivariateDistribution!Discrete, U = 
> getVariateType!D, T = GetDistributionParameterType!D)(D d, U x)
> if(!is(D == Poisson!T))
> {
>     assert(false, "density function unimplemented for this 
> distribution: " ~ D.stringof);
> }
>
> double density(D: UnivariateDistribution!Continuous, U = 
> getVariateType!D, T = GetDistributionParameterType!D)(D d, U x)
> if(!is(D == Gamma!T) && !is(D == Gaussian!T) && !is(D == 
> Uniform!T) && !is(D == Exponential!T))
> {
>     assert(false, "density function unimplemented for this 
> distribution: " ~ D.stringof);
> }
>

What you seem concerned about here is how to produce a meaningful 
error message for distribution that you do not have 
implementations for. A slightly more elegant solution would be to 
pack the structs into an AliasSeq and then use something like 
!allSatisfies to test them all. I'm sure there's a more elegant 
solution, but that's the first thing I thought of.

>
> immutable class(T...){...}
>
> that this class can only create immutable objects without 
> having to write immutable everywhere and or a UDA, but making 
> every member immutable accomplishes the same thing.
>

What you're looking for is an immutable constructor:

class C
{
     this() immutable;
}



More information about the Digitalmars-d-announce mailing list