Argument datatype dependant of previous arguments?
Argument datatype dependant of previous arguments? - Lemmy.world
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?