Skip to content

Instantly share code, notes, and snippets.

@yloiseau
Created January 15, 2016 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yloiseau/5dddafb35e13affdf9c5 to your computer and use it in GitHub Desktop.
Save yloiseau/5dddafb35e13affdf9c5 to your computer and use it in GitHub Desktop.
First order modules in Golo

Just a POC on a discussion we had with @Artpej

Given

module MyMod

function foo = |a, b| -> a + b

function bar = -> println("MyMod.bar")

we can do

module Test

function Module = |mod| -> DynamicObject()
    : fallback(|this, name, args...| -> fun(name, mod, args: length()): invoke(args))
    
function main = |args| {
  let m = Module(MyMod.module)
  
  println(m: foo(1, 2))
  m: bar()
}

or if you rather struct and augment

struct Module = { mod }
augment Module {
  function fallback = |this, name, args...| ->
    fun(name, this: mod(), args: length()): invoke(args)
}

🎉

@artpej
Copy link

artpej commented Jan 15, 2016

Excellent! 👍

@jponge
Copy link

jponge commented Jan 15, 2016

Don't tell @k33g_org 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment