Yeah, it’s a neat property that you can swap the implantation in Python easily by creating a Numpy-compatible package. Though one thing that bothers me with this approach is that you still need to edit the code. Of course, we can pass it around as an argument:
ismaxgreater(impl, f, g, xs) = impl.maximum(f, xs) > impl.maximum(g, xs)
ismaxgreater(f, g, xs) = ismaxgreater(ThreadsX, f, g, xs)
However, passing module as an argument in Julia causes type-instability. I think “impl
” object has to be something that has type-stable getproperty
(e.g., named tuple).
In JuliaLang/julia#34185 vchuravy suggested to use something like C++'s execution policies which has a similar outcome. I want do something similar with Transducers.jl too: Composable API for asserting algebraic properties and specifying execution strategy · Issue #143 · tkf/Transducers.jl. I think approach like this also helps us get something like ThreadsX.jl for Distributed.jl easily (freely?).
In terms of the syntax, I think impl.sort!(xs)
makes more sense (somewhat) than sort!(xs, impl)
as it’s more clear that the first syntax does not mutate impl
object. It also makes sense as impl
is just a “type-stable module”. It’s also nice that this can be done completely outside Base
. OTOH, this syntax creates a hard boundary between pre-defined API (e.g., sort!
and maximum
) between user-defined API; i.e., ismaxgreater
has to be called with impl
as an argument and not impl.ismaxgreater
. (Though I think it is possible to create a clever impl
object that overlays ismaxgreater
on top of existing impl
.)
ThreadsX is ignoring all these considerations and exposing the API in a dead-simple way as designing something like this takes time. I also think you don’t need this kind of API until there are multiple implementations that you can swap. I think it is also important to remind yourself that things passed to ThreadsX functions has to be “thread-friendly” (in the sense described in GitHub - tkf/ThreadsX.jl: Parallelized Base functions) when reading the code. I think an explicit prefix helps with it.