/**
 * @license
 * Copyright 2020 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.mlang;

import foam.core.ContextAware;
import foam.core.X;

public class ContextualizingExpr extends ProxyExpr {
  private final X x_;

  public ContextualizingExpr(X x, Expr delegate) {
    super(delegate);
    x_ = x;
  }

  public X getX() {
    return x_;
  }

  @Override
  public Object f(Object obj) {
    var result = super.f(obj);
    if ( result instanceof ContextAware ) ((ContextAware) result).setX(getX());
    return result;
  }
}

@@ -159,29 +159,19 @@
 ore.
-ContextAware;
+X;%0A
 %0Aimport 
 foam
@@ -158,32 +158,39 @@
 core.X;%0A%0Aimport 
+static 
 foam.core.X;%0A%0Apu
@@ -183,17 +183,47 @@
 am.core.
-X
+ContextAware.maybeContextualize
 ;%0A%0Apubli
@@ -474,132 +474,55 @@
     
-var result = super.f(obj);%0A    if ( result instanceof ContextAware ) ((ContextAware) result).setX(getX());%0A    return result
+return maybeContextualize(getX(), super.f(obj))
 ;%0A  

Use ContextAware.maybeContextualize