I have tried to use Julia module as static class. According to my
experience, it is doable. You cannot have two modules of the same names
though. When it comes to your example, it means you can't have two apples
(this apple and that apple).
I am afraid you should use immutable Apple type.
immutable Apple
s::float64
t::float64
Apple(a,b) = s > 0 ? new(a,b) : error("size should be positive")
end
Apple()= Apple(rand(),rand())
size(x::Apple) = x.s
taste(x::Apple) = x.t
a = Apple()
size(a)
taste(a)
On Thursday, May 12, 2016 at 4:26:50 PM UTC+2, Ford Ox wrote:
>
> I have checked julia git, but some questions still remain.
>
> Should I use module as class substitution? (Every global variable in
> module is encapsulated. Same for function unless one uses export).
> In that case where is my constructor?
>
> In OO languages you know exactly what methods every object has (in IDE you
> can use CTRL+Space). How can I do that, or should I need to do that in
> julia?
>
> Let's say I create a module called Apple, with methods taste() and size(),
> and somebody wants to use that module. Methods taste() and size() are
> obviously just getters for variables that are somehow stored in Apple
> module. The user should be never able to change size and taste of apple by
> himself, those values will be randomly set when constructor is called.
> How do I encapsulate those two variables? Where do I store those two
> variables (If I store them in type, user must be able to pass that type
> into Apple methods, thus he can just do AppleType.size = 6). How do I call
> constructor if variables are not stored in type?
> Note: I don't want to have immutable AppleType!
>