#1105 Acessing Intermediate objects in nested it-blocks

katox Tue 1 Jun 2010

Is there a way how to address enclosing-closure objects in a closure body instead of eclosing function object instance? I can create a local variable, let it autobind and use later but I wonder if there is a more elegant way...

#! /usr/bin/env fan

class B
{
  Void run()
  {
     a := A.make
     {
       echo("this=$this, it=$it")
       ainst := it
       it.test { echo("this=$this, it=$it, ainst=$ainst") }
     }
  }
}

class A
{
  Void test(|Int|? i := null) { i?.call(100) }
}

class Main
{

  static Int main()
  {
     B().run
     return 0
  }
}

In nested it-blocks it is quite common that I need the nearest "enclosing" object - despite it is only an it-block object.

brian Tue 1 Jun 2010

Yeap - you have to create a named local variable to access an "outer it", since each level will hide the it from the previous scope.

Login or Signup to reply.