Reputation: 1713
* PLEASE SEE END FOR IMPORTANT EDIT *
For various reasons I have something like:
delegate void Task(QueueTask queueTask);
delegate void QueueTask(Task task);
Using Visual Studio Express 2008 this works more or less fine apart from in lambda expressions where it can't deduce if I try call and call queueTask() in the function without explicit help:
void MyTask(QueueTask queueTask) { queueTask(qt => {}); // fails with queueTask doesn't take 1 parameter? }
This works though:
void MyTask(QueueTask queueTask) { queueTask((QueueTask qt) => {}); }
Its not a huge problem and I know I could also probably get away by passing in another object that contained the delegate instead but I was curious if there was a way to make it work as its stands?
Thanks.
* IMPORTANT EDIT BELOW *
It seems only if I put the class 'TaskPool2' in a separate assembly I get the compile error!?
namespace Test { public class TaskPool2 { public delegate void QueueTask(Task task); public delegate void Task(QueueTask queueTask); public void Queue(Task task) { } } } namespace MyApp { class Program { static void SomeFunc() { Test.TaskPool2 taskPool = new Test.TaskPool2(); taskPool.Queue(queue => { Console.WriteLine("A"); queue(q => Console.WriteLine("C")); }); } } }
Granted its somewhat convoluted!
Upvotes: 1
Views: 1578
Reputation: 3017
After using several ways to break\fix the code, I've found a small bit of information.
When the compiler reaches the line
taskPool.Queue(queue => { Console.WriteLine("A"); queue(q => Console.WriteLine("C")); });
it'll fall compilation if (and I think only if) it never encountered QueueTask before. For instance, declaring the next line in the class (before or after the SomeFunc method)
Public Delegate Test.TaskPool2.QueueTask x;
would fix the compilation. Also, adding the next line above the errornouse line will also fix it
Test.TaskPool2.QueueTask x; // declare x to be QueueTask
I've tried a few more lines using QueueTask, and they worked (fix the compilation).
I can only assume that the compiler doesn't load the definition for QueueTask, and this leads to this strangment.
Seems even like a bug.
Hope I managed to explain this well.
Upvotes: 1
Reputation: 1500515
(This would have been a comment, but it's not really feasible to give full code in comments.)
Your code already works as it stands:
delegate void Task(QueueTask queueTask);
delegate void QueueTask(Task task);
class Test
{
void MyTask(QueueTask queueTask)
{
queueTask(qt => {}); // fails with queueTask doesn't take 1 parameter?
}
}
Compiles with no problems.
Please create a similarly short but complete program which doesn't compile.
Upvotes: 0