Am I reaching limits of multiple dispatch?

Recently I implemented some basic cryptographic protocols. One of them is Diffie-Hellman key exchange protocol where only necessity is that the communication channel implements serialize and deserialize methods. Initially, I thought I would only need to import Serialization within my module, but soon I found myself in a situation where I needed to import my custom serializer within Diffie-Hellman module. That makes the module DiffieHellman more and more specialized where instead, I would like to strive for inclusiveness and compatibility.

How can I solve this issue? More precisely, how can I make the code DiffieHelmman.jl not to depend on SecureIO or Serialization and make modifications for the tests runtests.jl to work?

I’m not sure I properly understand what the actual question and problem are or how they relate to the limits of multiple dispatch, but if I understand correctly, you’re asking something along the lines of

I don’t like that my implementation of a function depend on specific serialization / deserialization functions from another package. I would like users to be able to choose what serialization / deserialization functions to use.

If that’s the case, you could always just add a keyword argument to your functions, i.e.

diffie(s,sign::Function,verify::Function,G::AbstractGroup,rng::AbstractRNG; 
       serializer::Function = Serialize.serialize, deserializer::Function = Serialize.deserialize)

then a user would write something like

using Some_Different_Serialization_Library: serialize, deserialize
diffie(s, sign, verify, G, rng, serializer=serialize, deserializer=deserialize)

If you don’t want your module to depend on Serialize, just don’t provide default keyword arguments, i.e.

diffie(s,sign::Function,verify::Function,G::AbstractGroup,rng::AbstractRNG; 
       serializer::Function, deserializer::Function)

Presumably a similar approach could be taken with your SecureIO dependancy.

You can also look into Requires.jl for ways to change the behaviour of your code depending on what packages the user has loaded.

4 Likes

I had not thought about putting them as arguments for the function. I kind was focusing on single <:IO single pairs of serializers. But it actually makes sense to disconnect them. Perhaps indeed that is the way to go :wink: