How to design a generic argument to a function

this post have a good explanation of anytype

the problem with anytype is the compiler need to knows what type its suppose to be at compile time, lets say i have 2 identities type ScryptIdentity and X25519Identity which one to use is determined at run time, with anytype i have to call encrypt 2 times.

encrypt(x25519_identity, data);
encrypt(scrypt_identity, data);

i can’t do something like this

const identity: anytype = parseIdentity(key); // this doesnt compile
encrypt(identity, data);

I can’t call the encrypt function multiple time for each identity type because the age encryption spec requires the final encrypted file to have all identities used be in the header, calling encrypt multiples time would result in multiple files with a single identity in the header instead.

I suppose splitting the encrypt function into multiple stages, which is something i already did for the header and payload stage, splitting the header part further so there is an addIdentity stage that can accept anytype can work, thank you for that idea. I’ll wait to see if others have any other idea before i implement it.

1 Like