Best way to group functions?

Since there are no classes in Julia, I wonder what might be a good way to group functions that work for the same type of objects?

One way I can think of is to use modules, just want to hear if there are other better approaches?

Thanks!

what is the meaning of “group” here, list them side by side? to discover them?

2 Likes

That is not a concept very natural in Julia. Functions are, whenever possible, written in a generic way, to be able to operate on types that any other user may define and where not even envisaged by the function original developer.

This talk gives an overview of the concept: JuliaCon 2019 | The Unreasonable Effectiveness of Multiple Dispatch | Stefan Karpinski - YouTube

4 Likes

Basically it means organizing functions such that it is easy to maintain and debug. Also, we can avoid passing the same argument through multiple functions if it is possible to define some “shared variables” (similar to OOP private variables of an object), which could make refactoring much easier.

structs already do that, the only difference is that in OOP:

OBJ.func(X)

in Julia:

func(OBJ, X)
3 Likes

You can put related functions close to each other in source. You don’t have to, of course. But you can.

2 Likes

Yes, that’s a good advice. I think the only place where I have seen a list of functions which relate to a type are the docs (or by looking into the source code).
So, if your package will have documentation, that might be the best place to communicate the groups of functions.

Even though generic functions are preferred, there are always a number of methods that have to be defined specifically for any new type, like accessors, or implementations of an interface you want to support.

Those methods ‘belong’ with the type, and it is probably meaningful to put them next to your struct definition. But you only ‘group’ them in the sense of putting them near each other in the source.

4 Likes