Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the <em>dynamic scope pattern</em>. More prosaically this means using a thread local variable (scala's <code>DynamicVariable</code> is done just for that) to store the current nesting level. See my answer to this other question for a partical example of this pattern: <a href="https://stackoverflow.com/questions/7069044/how-to-define-a-function-that-takes-a-function-literal-with-an-implicit-paramet/13252952#13252952">How to define a function that takes a function literal (with an implicit parameter) as an argument?</a></p> <p>This is suitable only if you want to know the nesting level for a very specific method though. If you want a generic mecanism that works for any method then this won't work (as you'd need a distinct variable for each method). In this case the only alternative I can think of is to inspect the stack, but not only is it not very reliable, it is also extremely slow.</p> <p><strong>UPDATE</strong>: actually, there <em>is</em> a way to apply the dynamic scope pattern in a generic way (for any possible method). The important part is to be able to implicitly get a unique id for each method. from there, it is just a matter of using this id as a key to associate a <code>DynamicVariable</code> to the method:</p> <pre><code>import scala.util.DynamicVariable object FunctionNestingHelper { private type FunctionId = Class[_] private def getFunctionId( f: Function1[_,_] ): FunctionId = { f.getClass // That's it! Beware, implementation dependant. } private val currentNestings = new DynamicVariable( Map.empty[FunctionId, Int] ) def withFunctionNesting[T]( body: Int =&gt; T ): T = { val id = getFunctionId( body ) val oldNestings = currentNestings.value val oldNesting = oldNestings.getOrElse( id, 0 ) val newNesting = oldNesting + 1 currentNestings.withValue( oldNestings + ( id -&gt; newNesting) ) { body( newNesting ) } } } </code></pre> <p>Usage:</p> <pre><code>import FunctionNestingHelper._ def decorate(f: =&gt; Unit) = withFunctionNesting { nesting: Int =&gt; println("I am decorated " + nesting + "x") ; f } </code></pre> <p>To get a unique id for the method, I actually get an id for a the closure passed to <code>withFunctionNesting</code> (which you must call in the method where you need to retrieve the current nesting). And that's where I err on the implementation dependant side: the <em>id</em> is just the class of the function instance. This does work as expected as of now (because every unary function literal is implemented as exactly one class implementing <code>Function1</code> so the class acts as a unique id), but the reality is that it might well break (although unlikely) in a future version of scala. So use it at your own risk.</p> <p>Finally, I suggest that you first evaluate seriously if Nikita Volkov's suggestion of going more functional would not be a better solution overall.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload