This page is no longer maintained — Please continue to the home page at www.scala-lang.org

implicit arguments and scoping

No replies
Ruslan Shevchenko 2
Joined: 2010-11-01,
User offline. Last seen 42 years 45 weeks ago.
Good day, community.

2 questions, related to interconnection between scala implicit parameters feature and scoping.


1. Are exists reason, why implicit syntax for arguments of anonymous functions is not supported ?
(I. e. it woulld be nice , to allow syntax like:
  Call{ (implicit x:T) =>  g(x) }
   and if g have implicit T argument - x will be substituted.

2. let we have sequence of scopes, where in scope X we have some implicit binding of type T to object v, and in inner scope we have  binding of variable with
the same name and type, but not implicit.  Then current rules will bind implicit parameters in inner scope to identifier from outer scope.  In theory it's ok, but
from practical point of view it's a great way for programmer to shoot yourself in the foot. 

 So, question:  It is possible to build some mechanism, which will prevent programmer from such type of errors (?)
I thinking about method for creating ambiguity in default inner scope, but yet without success.

// Illustration:
let's look on next code:
case class Context(val s:String, val l:Int)
{
  def next = Context(s,l+1);
}

object Env
{

  def executeInContext[A](f:Context=>A)(implicit ctx:Context) =
  {
    val next = ctx.next;
    println(next.s+","+next.l+"(");
    f(next);
    println(")");
  }

  def g(implicit ctx:Context):Unit = println("g:ctx.l="+ctx.l);

  def run() {
    executeInContext({
               ctx:Context => implicit val ictx = ctx;
                       println("a");
                       executeInContext({
                         ctx:Context => implicit val ictx = ctx
                           println("b");
                           g;
                       });
                       g;
                    }) (Context("c",0));
  }

}

And then on next modification of run:
    executeInContext({
               ctx:Context => implicit val ictx = ctx;
                       println("a");
                       executeInContext({
                         ctx:Context =>
                           println("b");
                           g;
                       });
                       g;
                    }) (Context("c",0));
  }


 here g in inner scope will bound  to ctx in outer scope.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland