Traits in Julia?

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?


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.


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

Also have a look at Methods · The Julia Language

1 Like

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.

1 Like