Argument datatype dependant of previous arguments?
While working with a dynamically typed lang, I came across this:
hash(password, algorithm, algorithmOptions)
Where algorithm is a constant signaling which hashing algorithm to use, and algorithmOptions is a dict whose keys depend on algorithm.
So I thought, can we dictate that if a previous parameter has this value, then this parameter has to have this other value?
E.g.
enum HashAlgo {
Bcrypt,
Argon2,
}
type BcryptOptions = {
Int optionA,
Int optionB,
}
type Argon2Options = {
String optionC,
String optionD,
}
// Here I make this type "depend" on an argument of type HashAlgo
type HashOptions = [HashAlgo] => {
HashAlgo::Bcrypt => BcryptOptions,
HashAlgo::Argon2 => Argon2Options,
}
fun hash(
String password,
HashAlgo algorithm,
// Here I use HashOptions, passing the prev. argument
HashOptions[algorithm] options,
)
This way the compiler can ensure the correct dict is used, based on the value of algorithm
Does something like this exist? I now realize that it would be impossible to type check in compile time based on a runtime value, but if it was allowed only for constants? What do you think?
Multiple ways you can do this. Most of these should also extend to multiple arguments, and although the constant is promoted to type level, you can pass it around nested functions as a type parameter.
With generics
In Java (personally I think this approach is best way to implement your specific example; also Kotlin, C#, and some others are similar):
In Haskell without GADTs (also Rust is similar):
In C (with
_Generic):In TypeScript, inverting which type is parameterized (see this StackOverflow question for another TypeScript approach):
With constant generics or full dependent types
This way is a bit more straightforward but also way more complicated for the compiler, and most languages don't have these features or they're very experimental. Dependent types are useful when your constant is non-trivial to compute and you can't even compute it fully, like vectors with their length as a type parameter and
appendguarantees the return vector's length is the sum. In that case generics aren't enough. Constant generics aren't full dependent types but let you do things like the vector-sum example.In Haskell with GADTs AKA Generic Algebraic Data types (also works in Idris, Agda, and other Haskell-likes; you can simulate in Rust using GATs AKA Generic Associated Types, but it's much uglier):
In Coq (also flipping the parameterized types again):
This is possible with c++ templates.
I think what you are referring to is "dependent types" They usually occur in more advanced functional programming languages like Idris or Agda but Zig also has them in a way
This would be trivial in python with something like
Of course it's python, so at runtime it wouldn't matter, but the static type checker would complain if you called
hashwithBCryptAlgorithmandArgon2Options. You could also have it return different types based on the arguments and then in call sites it'd know which type will be returned based on the type of the arguments. And only the last function has am implementation, the@overloadones are just type signatures.It's documented here.