I am currently designing a method that must be able to accept many different, but similar, encoders. The framework for this encoder is simple. It accepts a Vector of bytes and spits out a Vector of bytes.
How can I notate this kind of general behaviour in Julia?
Object-Oriented design does not work, because you can not define abstract types with functions like this
abstract Encoder
function encode(Vector{UInt8});
end
type MySillyEncoder <: Encoder
function encode(incomming::Vector{UInt8})
incomming
end
function doEncodig(encoder::Encoder, bytes::Vector{UInt8})
encoder.encode(bytes)
end
Functional design does not work, because you can not define the arguments of a function-parameter like this
function mySillyEncodeFunction(bytes::Vector{UInt8})::{Vector{UInt8}
bytes
end
function doEncodig(encode::Function{Vector{UInt8}, Vector{UInt8}}, bytes::Vector{UInt8})
encode(bytes)
end
doEncodig(mySillyEncodeFunction, ...)
Now since I can not guarantee that the object or function provided is actually confirming to my input and output rules all I can do is hope that the caller is doing it right. But the problem here is that if the caller does not do it right, all he can do is read examples or try to find some documentation about what we expect his encoder to look like.
Not being able to enforce behaviour and deduct that behaviour from signatures will make a large codebase brittle and easily breakable and hard to understand.
I understand that I could do the functional design path without specifying the types of the encode-function and that it will crash once typemissmatch occurs, but that does not solve the problem of the reader of my doEncoding function will not know what the input and output-types of my encode function are. He will have to slough trough my code and
will have to understand, more likely guess, as to what the encode function should do, accept and return.
I could also sprinkle Asserts and checks everywhere to help the caller, but this seems like something the language should provide.
Please understand that that encode is just an example here for much more complex function.