Traits in Julia?


#1

Is there an idiomatic way to do user defined traits ala Scala or Rust, or interfaces ala Java?

I want to define ‘contracts’ so I know which functions are available for my arguments. Julia has predefined interfaces such as Iteration. How does one declare new ones?

Thanks
P


#2

There is no first class “Iteration” concept in Julia—it’s just a convention with documentation. That’s the current approach to this sort of thing, similar to duck typing in other dynamic languages. We would like to add a first-class interface/protocol/concept entity at some point, but it was beyond the scope of the 1.0 release of the language.


#3

OK, thanks. I will ‘duck’ for now and look forward to some future release


#4

Also have a look at https://docs.julialang.org/en/v1/manual/methods/#Trait-based-dispatch-1


#5

You can define your own in the same way the “Iteration” or “AbstractArray” conventions are defined.
Ie, with documentation and a host of methods that will work on any type with which it is defined.

mauro3 also created the SimpleTraits.jl library.