On Thursday, 24 August 2017 at 20:11:32 UTC, data pulverizer
wrote:
Thanks. I think most of that is down to D's nice syntax and how
it easily and clearly emulates Julia. I think that there is
certainly more to say on this especially around strategies of
how represent concrete types. David Gileadi's point about UDAs
could add an interesting spin on things, and Ali's point on
dynamic dispatching.
On UDAs, at least in the current implementation, I think that the
actual issue you are trying to address is to force the type in
the distribution to be convertible to double in the continuous
case and convertible to long in the discrete case. All things
considered, that can be implemented with template constraints, as
in
class Gamma(T):
if(isFloatingPoint!T)
{
immutable(T) shape;
immutable(T) scale;
this(T shape, T scale)
{
this.shape = shape;
this.scale = scale;
}
}
though you could probably take a more abstract approach. (I'm
also not 100% on having immutable member variables). Also,
density's signature could then avoid the template constraint.
auto density(D: Gamma!T, U : T, T)(D d, U x)
Even better, if you're calling the dstats functions, you could
re-write density as something like
auto pdf(D: Dist!T, U : T, Dist, T)(U x, D d) {
mixin("return x." ~ lookupdstatdensity!Dist ~ "(" ~
stringmembersofd ~ ")";
}
and create a lookupdstatdensity function that returns a string of
the relevant dstats function at compile-time (and a function
returning a string of the members of d) (I also would re-name
density to pdf and switch the order of x and d). This would
probably be the most DRY approach.
On Ali's point on dynamic dispatching, Julia is a scripting
language with a JIT compiler. So if you call a function with some
types known at compile time and the overload exists, it will
compile the correct version of the function for the relevant
types. It will then cache that so that if you need it again you
don't pay any additional cost. So it's similar to what you're
doing on that respect. However, there is a runtime dispatch
component that would take something like openmethods to
implement, I think.