duby icon indicating copy to clipboard operation
duby copied to clipboard

Closures blocking out the surrounding scope for classes/instances

Open vito opened this issue 15 years ago • 0 comments

An example, with the Android API:

hello = self
btn.setOnClickListener do
  def onClick(v)
    return if hello.txt.getText.toString.length == 0
    hello.addMessage("hellotester", Hello(v.getContext).txt.getText.toString)
    message = Bundle.new
    message.putString("say", Hello(v.getContext).txt.getText.toString)
    Message m = Message.obtain
    m.setData(message)
    hello.client.handler.sendMessage(m)
    hello.txt.setText("")
  end
end

hello is not accessible. Workaround here is to use Hello(v.getContext) (where Hello is the name of the class).

vito avatar Apr 14 '10 20:04 vito