#481 Injecting closure for getter

freddy33 Fri 6 Mar 2009

I really like this idea of having lazy "once get" for connecting my objects. Futhermore if I can inject a closure for the getter, than I can really have clean state isolation between my object life cycle. What I mean is that having a property or a member does not mean it's life is link to me :)

Today, the only way I found to do it is:

class HelloWorld
{
  static Void main(Str[] args)
  {
    h := HelloWorld()
    p := Person { initName = &h.myName }
    echo("Hello ${p.name}")
  }

  once Str myName()
  {
    return "Fred"
  }
}

class Person {
  |->Str| initName
  Str name {get {return initName.call([,]) }}
}

Is there a better way?

tompalmer Fri 6 Mar 2009

Ignoring your real question (haven't spent the time to understand it), you could shorten your once method to this now:

once Str myName() {"Fred"}

brian Sat 7 Mar 2009

Not sure exactly what you are trying to do there, but I'm guessing what you are really trying do is setup delegation with closures. I think basically if you want to have a real field and a pluggable function, you will probably need two slots to do it. So I don't have any recommendations.

Login or Signup to reply.