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?
Maybe not as much, it would seem that way, but the same thing happened with Sync last month and people were defending the app, ads & google because the app is prettier, more convenient or familiar.