Let’s suppose I have an object that has cooties. Potentially I may have all of the following at one place or another:
- A struct with a property called
cooties. - An access method called
cooties(let’s limit ourselves to getting cooties, not setting them, which is bullying). - A keyword argument called
cootiesfor a constructor method. - Code that uses
cootiesfor a variable that will supply the cooties of a new object.
Uses 1–3 coexist in peace. I am happy with the following:
struct Kid
name::String
age::Int
cooties::Int
function Kid(name::String; age::Int, cooties::Int=0)
new(name, age, cooties)
end
end
name(k::Kid) = k.name
cooties(k::Kid) = k.cooties
new_kid = Kid("Kevin"; age=7, cooties=4)
if cooties(new_kid) > 1
println("Ew! $name(new_kid) has cooties!")
end
But use case #4 is impossible, since cooties is now a method name in this scope. This is the price Julia pays for refusing the traditional object.method syntax.
Here are the alternatives that come to mind:
A. Put the Kid in a module and use import with fully qualified names at all times, or when you are anticipating this clash. In my MWE, that is a fine way to go, but it is less appealing when the module name is RationalFunctionApproximation. Also, if you are in a module already, it leads in the direction of a module hierarchy that otherwise seems superfluous.
B. Prefix all access methods with get_ and set_. A sensible solution without much extra wordiness and with the added benefit of tidier tab completions. But I don’t see it in most Julia packages. In fact, it seems to contradict what Blue style says.
C. Use more descriptive variable names, like the_cooties = 4 or I_hear_the_new_kid_has_this_many_cooties = 4. I should probably code like this anyway, right? Right?
D. Suffix the access methods, i.e., ageof() and cootiesof(). I suspect this would be awful and we should continue to pretend that typeof didn’t happen.
E. Access methods are for weenies, just use direct property access. Requires a syntactic difference between real properties and virtual ones, and it’s messy for compound or nested types.
Have I missed one? Does the Julia community have a clearly preferred option? I suspect, but I can’t prove, that it’s “we gave you namespaces so that you would use A.”