Why is this error "local variable cannot be declared in this scope because it is ... already in use"?

I am trying to run the code from Eric Lippert's blog article "Why does a recursive lambda cause a certain assignment error?"

but instead of running (or giving compilation a "definite assignment error", I get:

A local variable named 't' cannot be declared in this scope because it would mean a different value for "t" which is already being used in the "parent or current" scope to denote something else

Why?
Where is it already in use in the parent or current scope?
Tried to rename it getting the same error
How do I change the code to run this code?

using System;
namespace LippertRecursiveLambdaError
{
  class Program
  {
    static void Main(string[] args)
    {
      Tree<int> tree = new Tree<int>
      (3, new Tree<int>
             (4, new Tree<int>(1), null), 
          new Tree<int>(5));
      PrintTree(tree);
      Console.ReadLine();
    }

    delegate void Action<A>(A a);
    delegate void Traversal<T>(Tree<T> t, Action<T> a);

  static void DepthFirstTraversal<T>(Tree<T> t, Action<T> a)
  {
    if (t == null) return;
    a(t.Value);
    DepthFirstTraversal(t.Left, a);
    DepthFirstTraversal(t.Right, a);
  }
  static void Traverse<T>(Tree<T> t, Traversal<T> f, Action<T> a)
  {
    f(t, a);
  }
  static void Print<T>(T t)
  {
    System.Console.WriteLine(t.ToString());
  }
  /*static void PrintTree<T>(Tree<T> t)
  {
    Traverse(t, DepthFirstTraversal, Print);
  }*/

  static void PrintTree<T>(Tree<T> t)
  {
    //Traversal<T> df = (t, a)=>          **************
    Traversal<T> df = null;
    //========================================
//The next line gives compilation error
//A local variable named 't' cannot be declared in this scope 
//because it would give a different meaning to 't', 
//which is already used in a 'parent or current' scope to denote something else       
    df = (t,
      a) =>
     {
       if (t == null) return;
       a(t.Value);
       df(t.Left, a);
       df(t.Right, a);
     };
  Traverse(t, df, Print);
  }//PrintTree
  }//class
  class Tree<T>
  {
    public Tree<T> Left;
    public Tree<T> Right;
    public T Value;

    public Tree(T value) 
    { 
       Value = value; 
    }
    public Tree(T value, Tree<T> left, Tree<T> right) 
    { 
        Value = value; 
        Left = left; 
        Right = right; 
    }
  } 
}//namespace

      

+3


source to share


1 answer


  static void PrintTree<T>(Tree<T> t)
  {
    //Traversal<T> df = (t, a)=>          **************
    Traversal<T> df = null;
    //========================================

    df = (t,      a) =>
     {
       if (t == null) return;
       a(t.Value);
       df(t.Left, a);
       df(t.Right, a);
     };
    }

      

This is because Tree<T> t

- one declaration AND (t,a) =>

is another declaration .. pretty much the same as saying:

int someFunction(T t, U a)
//Assuming int is the return type

      



Anyway, to fix: change t

to a different id .. n

eg

df = (n,a) =>{
           if (n == null) return;
           a(n.Value);
           df(n.Left, a);
           df(n.Right, a);
         };
        } 

      

+5


source







All Articles