I have searched docs but didn't find any info on this.
How should one structure larger project (say minesweeper) with data encapsulation on mind?
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)
Do a search for “encapsulation” in this google group and you’ll find quite a number of discussions on some of the design philosophies around this topic, many from the julia devs.
julia> type Car end
julia> speed(::Car) = 60
speed (generic function with 1 method)
julia> type Plane end
julia> speed(::Plane) = 500
speed (generic function with 2 methods)
julia> function move(pointA, pointB, tools...)
speeds = map(speed, tools)
idx = findmax(speeds)[2]
println("""I don't always go fast, but when I do, I prefer a $(lowercase(string(typeof(tools[idx])))).
I am... the most interesting coder in the world.""")
end
move (generic function with 1 method)
julia> move(1, 2, Car(), Plane())
I don't always go fast, but when I do, I prefer a plane.
I am... the most interesting coder in the world.
julia> type Teleporter end
julia> speed(::Teleporter) = Inf
speed (generic function with 3 methods)
julia> move(1, 2, Car(), Plane(), Teleporter())
I don't always go fast, but when I do, I prefer a teleporter.
I am... the most interesting coder in the world.
module Debug
include("AST.jl")
using Debug.AST
end
module...
export...
type Foo x end
type Holder{T} x::T end # don't export this one
setfield(f::Foo, key, value::Holder) = f.key = value.x
setfield(f::Foo, key, value) = raise error...
# Other functions...
end
Only functions from module will be able to wrap values into Holder, so all methods defined elsewhere will raise error when trying to modify Foo variables...