Skip to content

Instantly share code, notes, and snippets.

@titimoby
Created June 23, 2015 14:29
Show Gist options
  • Save titimoby/2f72a7ee851bb641571a to your computer and use it in GitHub Desktop.
Save titimoby/2f72a7ee851bb641571a to your computer and use it in GitHub Desktop.
je dois comprendre, c'est tout ;)
module goloid
import gololang.Adapters
function tryit = |adapter| {
var b = 1
adapter: interfaces(["java.lang.Runnable"])
: implements("run", |this| {
b = 2 # ceci résulte en une belle erreur :
# [error] Assigning `b` at {line=10, column=7} but it is a constant reference
println("new implementation")
})
return adapter
}
function main = |args| {
println("start")
tryit(Adapter())
}
@k33g
Copy link

k33g commented Jun 23, 2015

en fait tu ne peux pas modifier dans une closure ce qui a été défini en dehors

du coup utilise des maps ou des DynamicObjects

@yloiseau
Copy link

Ou alors, ce que je fais (c'est un peut sale), c'est ça:

struct Ref = { value }

function closure = {
  let extern = Ref(1)
  return {
      println(extern: value())
      extern: value(2)
      println(extern: value())
  }
}

function main = |args| {
  let myclosure = closure()
  myclosure()
}

on peut aussi passer par une liste à un seul élément (c'est le pattern classique en python), ou n'importe quel objet qui va encapsuler la valeur destinée à changer, mais je trouve plus clair la structure référence.

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