Re: Accessing functions inside of functions

84 views
Skip to first unread message

Nate W

unread,
Jan 14, 2013, 5:28:46 PM1/14/13
to juli...@googlegroups.com
So this is literally just an hour or so of playing around but should give a better idea:


Also, glad to be in the community!  I've followed the language since that first blog post and been wanting to jump in.  With the Package manager and packages for ODBC, Mongrel2, and HTTP I felt it was time to give it a go.

On Monday, January 14, 2013 2:11:17 PM UTC-8, Nate W wrote:
I'm sure this is an anti-pattern or perhaps just implemented totally wrong, but I've begun hacking on a simple MVC framework for Julia and I'm exploring how to structure the M and C aspects of it.

I'm taking inspiration from a few places, namely the simplicity of web.py, but the general structure and syntax of Rails appeals to me.  I'd love to be able to do something like this in a controller file:

App.controller("Posts") do
  function index()
  end
end

As you can guess the App module would store the logic for the entire app, with the controller part looking something like this:

module App
  function controller(actions, name::String)
    println("Adding controller $name, $actions")
    _controllers[name] = actions
  end
end

Now, I'm definitely using this as a learning experience as I only have experience using MVC rather than creating one.  But, my simple understand would be at some point I hit a route that maps to that controller and action, and I'm going to want to call it.

Given that my action is a function inside of a function (using the syntax I'm attempting here), what would be a clean way to store this?

My hope was to store all the functions inside of controllers in a Dict ("controller#action" => function) which would make it extremely simple to call them.  But I'm guessing there is no way for me to access and store the functions inside of the controller function.

My guess at this point is that I'll have to pass in the actions differently, unless there is some way to run a function inside a function arbitrarily ( totally incorrect I'm sure, but something like exec("function.function()")? )

Basically, looking for any advice on which way to take this! :)

John Myles White

unread,
Jan 14, 2013, 5:33:15 PM1/14/13
to juli...@googlegroups.com
I'm not sure this is the right way to structure things. It's been a long time since I thought about MVC systems, but I think what you want is to a build a controller object and define methods for it. Rails is so nice because of how many controller object methods can be inherited.

  -- John

--
 
 
 

Nate W

unread,
Jan 14, 2013, 5:35:44 PM1/14/13
to juli...@googlegroups.com
So this is exactly what I'd like, but without classes I wasn't sure how to implement this.  A new type?

Nate W

unread,
Jan 14, 2013, 5:38:51 PM1/14/13
to juli...@googlegroups.com
Ah ok, I think I can see how.  Make a module that can be included into a controller.  And then pass that to the app somehow.


On Monday, January 14, 2013 2:33:15 PM UTC-8, John Myles White wrote:

John Myles White

unread,
Jan 14, 2013, 5:41:24 PM1/14/13
to juli...@googlegroups.com
Yes. You should think of Julia types as classes, with the exception that abstract types can't be instantiated. I would do something like:

abstract Model
abstract View
abstract Controller

Then start defining functions on these:

function index(c::Controller)
  foo()
end

Relevant will be the ability to call a method from higher up in the type hierarchy using invoke():

function index(c::MyController)
  bar()
  invoke(index, (Controller, ), c)
end

 -- John

--
 
 
 

Nate W

unread,
Jan 14, 2013, 5:59:37 PM1/14/13
to juli...@googlegroups.com
Ok.  So with that structure, how would I allow people to be able to define their own functions with whatever names they want within a controller.

And looking ahead (if you check out the repo you can see what I'm attempting more clearly in code), how would I then allow the models to be usable within controllers.  I'd love to be able to do something like this (all theoretical code here):

# Controller
App.controller("posts") do
  using App.Controller # gives us render, etc

  function index()
    render(Post.title())
  end
end

# Model
App.model("Post") do
  # something
  function title()
    "Title of post"
  end
end

I know this code is rather funny in how naiveté it is. Perhaps I should start a new thread to get some better direction on this?

Patrick O'Leary

unread,
Jan 14, 2013, 6:04:28 PM1/14/13
to juli...@googlegroups.com
On Monday, January 14, 2013 4:59:37 PM UTC-6, Nate W wrote:
I know this code is rather funny in how naiveté it is. Perhaps I should start a new thread to get some better direction on this?

If you do, could you please open it on julia-users instead of julia-dev?

I do wonder if there's an application model that feels more natural under multiple dispatch.

John Myles White

unread,
Jan 14, 2013, 6:07:36 PM1/14/13
to juli...@googlegroups.com
They can just define functions. The concept of a function within a controller is really important in Ruby because Ruby is so object-oriented. Because Julia uses multiple dispatch instead of objects, you can just let users define functions that operate on their own models.

I'm a little confused what you're trying to do with the `do` blocks in your examples.

Opening a new thread would be a good idea. I'd also encourage you to look into how an MVC system is built before trying to do it in Julia. It's definitely a great learning experience, but  it will take a long time if you have to learn a new language and a new infrastructure at the same time. Borrowing ideas from Ruby or Python will help.

 -- John

--
 
 
 

Nate W

unread,
Jan 15, 2013, 10:08:46 AM1/15/13
to juli...@googlegroups.com
If you've worked with Rails you can see I was basically translating the Rails MVC structure directly to Julia, at least syntactically.  That was why I asked about functions inside of functions in the thread because I was hoping I could get a list of their names to pointers somehow.  I was just using it as a jumping off point.

I opened a couple more general threads in julia-users hoping to get some feedback on wrangling Julia into this task.

Also, I was a little miffed by your reply.  Besides working with them for years I have in fact looked at a number of MVC's and understand how they work, but they are all class/inheritance based.  I spent some time last night looking at more prototypical languages as well but couldn't find anything that really helped.  In the future it would have been more helpful to suggest what exactly you think I'm missing or where to look, instead of just implying I don't understand some fundamental thing.

Nate W

unread,
Jan 15, 2013, 10:12:43 AM1/15/13
to juli...@googlegroups.com
I will defer future questions for julia-users, but as this is already here I figure I should ask:

In this example, the index() method is pre-defined and then the user would to extend it from what I'm understanding.  But I wouldn't be assuming anything about what functions they use, instead just providing the user with some routes to map between path => function, which they define on their own.  This seems like I'd have to predefine all the functions, or am I missing it totally?
Reply all
Reply to author
Forward
0 new messages