Why will we have to say fieldnames(typeof(v)) instead of fieldnames(v)?

Julia’s dispatch mechanisms allow you to define different implementations based on the input type, but the guiding principle should be that all the methods for a given function should mean the same thing. Obviously that’s pretty loosely-defined, so I try to think about what the help string for a given generic function should be, and minimise the number of “except when” clauses you’d need.

So in this case it seems like the new behavior is “fieldnames(x) returns a list of the fields of x”. You’re arguing for

fieldnames(x) returns a list of the fields of typeof(x). fieldnames(x::Type) returns the fields of x.”

This sort of punning is discouraged because while it may make things more convenient when you know a prori the type of x, it makes it harder to write generic code that means the same thing regardless of the type of x.

9 Likes