Julia: Enforce constraints on objects in a container? -
i rather new julia; programming typically in c++, python, or fortran numerics. understanding julia lacks analogous c++'s private variables (or python's "i suggest treat private" convention of using leading underscore). if have container, there way enforce constraints on objects add container?
consider example: let's want array of integers, , constraint integers in array must share greatest common factor greater one. if put 12 array, number that's multiple of 2 or 3 may added. next add 21, , greatest common factor must 3. if try add 26, error because violates constraint. had added 12 26, legal greatest common factor of 2.
i realize it's bit of contrived example, should have salient features of hope do, , requires less explanation.
true enforcement possible immutable
types, can check desired constraints in inner constructor(s). outside type definition there no way add new inner constructors, , if there 1 cannot create instance without going through one.
however, while convention in python fields begin _
private, general convention in julia fields private (unless explicitly documented). it's considered bad style access fields directly outside implementation of type, should have accessor functions instead.
Comments
Post a Comment