Let’s say I have a library with a function, which accepts a structure name like: TestData. This function queries some data from the database and then returns with a TestData structure with the data inside by calling it’s TestData.new/1 function. Is there any way to enforce this function to accept only structures that have the new function implemented? My initial though was to use protocols, though it’s not working, because protocols work on structure objects, not on module names. Or if not, is there some idiomatic way to implement something like this?
In this case if I create a behaviour how could I enforce that the function parameter implements it? IIRC we can’t pattern match for behaviours? Or the only way to indicate to the users of the library that this function accepts structures in the parameter which implements this behaviour is a function doc?