Julia large project example.

1,529 views
Skip to first unread message

Ford Ox

unread,
May 12, 2016, 4:45:47 AM5/12/16
to julia-users
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?


Do you have link to any existing project, or could you provide an interface or graph, showing, how would you implement ^?


All projects I have written so far, feel like a mess ( pretty much like android app developing :P ).

Páll Haraldsson

unread,
May 12, 2016, 5:37:58 AM5/12/16
to julia-users
On Thursday, May 12, 2016 at 8:45:47 AM UTC, Ford Ox wrote:
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?

Funny you should mention Minesweeper and "larger" in the same sentence, as the 80-line Minesweeper in my favorite example of concise Julia code to show:

http://escher-jl.org/ *

https://github.com/shashi/Escher.jl

This includes all the code (excluding this generic library, and dependencies), and doesn't need JavaScript, HTML, CSS..


In general, similar to Python, you can access data members of your types (classes in Python), and there is no "private" as in C++ (or "protected", that wouldn't apply here), but it's bad practice to do that.

There are very large Julia projects, so this is not a hindrance. See also:

https://en.wikipedia.org/wiki/Composition_over_inheritance


* Very strangely, the website comes up, then turns blank, at least in Firefox. I believed I had screwed up, something in the browser, with some add-on, but in fact, I happened to get a new machine, and now have a clean profile. This used to work..

 

Ford Ox

unread,
May 12, 2016, 6:05:17 AM5/12/16
to julia-users
Well, the minesweeper was indeed a bad decision. I am looking for project with at least 5 modules.

Steven G. Johnson

unread,
May 12, 2016, 8:46:01 AM5/12/16
to julia-users
The largest Julia project is currently Julia itself, which is mostly written in Julia.
Message has been deleted

Ford Ox

unread,
May 12, 2016, 10:26:50 AM5/12/16
to julia-users
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!

Ford Ox

unread,
May 12, 2016, 10:29:17 AM5/12/16
to julia-users
I would expect that those questions will be answered in documentation in bold even if the answer is: No, you can't do that!

Sisyphuss

unread,
May 12, 2016, 11:29:01 AM5/12/16
to julia-users
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)

David P. Sanders

unread,
May 12, 2016, 12:06:44 PM5/12/16
to julia-users
In this example you can easily create another, distinct, object of the same type with a2=Apple().

Ford Ox

unread,
May 12, 2016, 12:18:34 PM5/12/16
to julia-users
That was exactly what I didn't want to see. This approach is so dirty that I feel like earth worm just by looking at this.
I am pretty sure that julia devs share my disgust :)

Dne čtvrtek 12. května 2016 17:29:01 UTC+2 Sisyphuss napsal(a):

Kristoffer Carlsson

unread,
May 12, 2016, 12:52:22 PM5/12/16
to julia-users
I am pretty sure the Julia developers can speak for themselves. A more humble approach would suit you well.

Tom Breloff

unread,
May 12, 2016, 12:54:05 PM5/12/16
to julia-users
That's quite the understatement Kristoffer...

Ford Ox

unread,
May 12, 2016, 1:19:31 PM5/12/16
to julia-users
I am sorry for those words. The idea of that sentence should have been:

"Your approach looks like a big no no.
Could julia devs share their idea of how should apple encapsulation achieved? Since they are the one, who invented this language, they had to consider encapsulation many times already, so they should be the very first person who gives advice on this particular topic (since nobody answered this topic well one comes to conclusion that they are also the only one who can answer it)."

Thank you for your time and patience devoting to my questions.

Dne čtvrtek 12. května 2016 18:52:22 UTC+2 Kristoffer Carlsson napsal(a):

David Anthoff

unread,
May 12, 2016, 1:40:06 PM5/12/16
to julia...@googlegroups.com

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.

Ford Ox

unread,
May 12, 2016, 3:34:23 PM5/12/16
to julia-users
I did read those and it didn't make it much clearer to me. (Hell I didn't even know what is coupling until this Thursday morning)
That's why am I asking for example and possibly some page on this topic in docs, since I have been programming in OO languages all my short programmers life and Julia comes with completely different approach.
Some things from OO can be achieved by different approach, some things can't be achieved and shouldn't be even tried to.
I don't want to reinvent the wheel. I don't wanna try make Julia OO. But I want coupling / nicely distributed code into multiple files (that's why OO exist in the first place right?).

And I am sure that many people joining julia (especially with v1.0) will be asking the same question. Therefore I would expect some official guide at one visible place (which this thread is no more).

Dne čtvrtek 12. května 2016 19:40:06 UTC+2 David Anthoff napsal(a):

Tom Breloff

unread,
May 12, 2016, 4:10:22 PM5/12/16
to julia-users
I find the most valuable thing to do when designing julia code is to focus purely on "verbs", not "nouns".  This means focusing on the action... NOT the object that is acting.  In julia, you can do fun stuff like:

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.

The point with this silly example is that, in OO, you would first start thinking about all the things that your transportation tools can do, and about heirarchies of "well, a plane is kind of like a car, and, well, kind of like a bird.  I know, I'll inherit from both!"  But really you should think about the important actions and attributes, and the hierarchy and interactions between objects will follow naturally. 

Scott Jones

unread,
May 12, 2016, 4:16:53 PM5/12/16
to julia-users
Very good advice - is anything like it already in the Julia docs?  If not, it should be added, up near the beginning.

Sisyphuss

unread,
May 12, 2016, 5:48:11 PM5/12/16
to julia-users
This sure is valuable advice.

But I'm not sure whether in v1.0, it will be the same case.

Sisyphuss

unread,
May 12, 2016, 5:55:50 PM5/12/16
to julia-users
I think what is missing in the docs is something like "package design guideline".

Tim Holy

unread,
May 12, 2016, 6:25:43 PM5/12/16
to julia...@googlegroups.com
It's been a while, but long ago I remember noticing that Debug.jl has a more
fine-grained module structure.

Best,
--Tim

On Thursday, May 12, 2016 12:34:23 PM Ford Ox wrote:
> I did read those and it didn't make it much clearer to me. (Hell I didn't
> even know what is coupling until this Thursday morning)
> That's why am I asking for example and possibly some page on this topic in
> docs, since I have been programming in OO languages all my short
> programmers life and Julia comes with completely different approach.
> Some things from OO can be achieved by different approach, some things
> can't be achieved and shouldn't be even tried to.
> I don't want to reinvent the wheel. I don't wanna try make Julia OO. But I
> want coupling / nicely distributed code into multiple files (that's why OO
> exist in the first place right?).
>
> And I am sure that many people joining julia (especially with v1.0) will be
> asking the same question. Therefore I would expect some official guide at
> one visible place (which this thread is no more).
>
> Dne čtvrtek 12. května 2016 19:40:06 UTC+2 David Anthoff napsal(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.
> >
> >
> >
> > *From:* julia...@googlegroups.com <javascript:> [mailto:
> > julia...@googlegroups.com <javascript:>] *On Behalf Of *Ford Ox
> > *Sent:* Thursday, May 12, 2016 10:20 AM
> > *To:* julia-users <julia...@googlegroups.com <javascript:>>
> > *Subject:* [julia-users] Re: Julia large project example.
> >
> >
> >
> > I am sorry for those words. The idea of that sentence should have been:
> >
> > "Your approach looks like a big no no.
> > Could julia devs share their idea of how should apple encapsulation
> > achieved? Since they are the one, who invented this language, they had to
> > consider *encapsulation* many times already, so they should be the very

Sisyphuss

unread,
May 13, 2016, 4:54:01 AM5/13/16
to julia-users
In Debug.jl:
module Debug
include("AST.jl")
using Debug.AST
end

After the `include`, `AST` is already in `Debug.jl`'s global scope, why it needs to `using Debug.AST` instead of `using AST`? 

Ford Ox

unread,
May 24, 2016, 4:03:50 AM5/24/16
to julia-users
A little bit more on the topic of encapsulation, if somebody desperately wants it.

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...

Scott Jones

unread,
May 24, 2016, 9:13:12 AM5/24/16
to julia-users
What effect does that have on performance? (Of course, I want to have my cake and eat it too!)
Reply all
Reply to author
Forward
0 new messages