..以及薯條的一側。C#使用泛型方法的MonoTouch方法重載和使用默認參數的非泛型方法
我有一個代碼庫,我正在爲Windows和MonoTouch編譯。在早晨的凌晨我編碼類似,其彙編了MonoTouch的,但無法在Windows上人爲的例子:
void Run()
{
// okay on both
exec("hello", 1);
// okay on MonoTouch
// compiler error on windows
exec("hello");
}
interface IFace { void foo(); }
void exec(string s, int n=0)
{
Console.Write("A");
}
void exec<T>(T t) where T:IFace
{
Console.Write("B");
}
在MonoTouch的,這個編譯和運行,打印:
AA
在Windows上,這個例子給出了一個編譯時錯誤:
The type 'string' cannot be used as type parameter 'T' in the generic type or method 'App.Program.exec<T>(T)'. There is no implicit reference conversion from 'string' to 'App.Program.IFace'.
上7.4.2 Overload Resolution C#的規範說的7.4.2.1 Applicable function member必須的參數相同數量:
The number of arguments in A is identical to the number of parameters in the function member declaration. 7.4.2.1
所以它似乎MonoTouch的編譯器正在考慮缺省參數適用函數成員搜索時,但Windows編譯器是沒有的。所以,候選函數成員是:
// exec with no default parameters. not applicable because no int supplied
void exec(string,int);
// exec with default value for the second parameter.
// only considered on MonoTouch.
void exec(string,int=0);
// generic exec with string as the type, which is invalid
// due to the IFace constraint. Invalid on both platforms.
void exec<string>(string) : where T:IFace;
所以,這是在MonoTouch的適用功能搜尋的錯誤,還是應該在Windows編譯器認爲是有效的默認參數的非泛型方法?
乾杯, 釐米
EDIT dlev
的回答後,我檢測了兩個約束和不受約束通用方法,並且它出現在單聲道編譯器選擇正確的方法中的無約束的情況下。在受約束的情況下,Mono編譯器似乎在約束失敗時正在考慮約束或回溯來尋找替代方案。
問題/錯誤減少到:
void Run()
{
foo(1);
bar(1);
}
void foo(int a, int b = 0) { print("A"); }
void foo<T>(T t) { print("B"); }
void bar(int a, int b=0) { print("X"); }
void bar<T>(T t) where T : IFace { print("Y"); }
在Windows和MonoTouch的,foo
正確打印B
。但bar
無法在Windows上編譯,但在MonoTouch上打印X
。
EDIT2 對於那些有興趣的人,我的解決方案是刪除默認參數,並要求顯式調用。在我的實際系統中,類型約束指定了兩個接口,所以我不能輕易將通用調用更改爲exec(IFace t) { ... }
。我想我可以重構,但是這是我的系統的肉類和下面的解決我目前的編譯問題:
void exec(string a) { exec(a,0); }
void exec(string a, int b) { ... }
void exec<T>(T t) where T : IFace, IFace2 { ... }
雙乾杯,釐米
[錯誤報告6056(https://bugzilla.xamarin.com/show_bug.cgi?id=6056)提交xamarin: – cod3monk3y 2012-07-10 02:12:42