c# - Why Can't generics Actions written in code need explicit parameter declaration -
for reason have feeling jon skeet know answer it's worth shot.
i have method stub used proxy generate commands.
public static void setcommand<t>(string commandname, action<t> execution) t : new() { //omitted unimportant }
i have function calling code, behavior differs depending on how it's called.
if call code explictly declaring action of parameter can resolve fine
action<classdeclarationoptions> test = (t) => { }; setcommand(generate_class_command, test);
however if declare function represent action
public static void generateclass(classdeclarationoptions classoptions) { }
then need explictly declare parameter when passing function so:
setcommand<classdeclarationoptions>(generate_class_command, commands.generateclass);
can explain why compiler cannot resolve generic parameter method definition
let's there's overload on commands.generateclass
takes different parameter type, example:
public static void generateclass(someotherclass stuff) { }
now there's no way compiler figure out generateclass
method use. however, specify type parameter ambiguity goes away.
so if had single method , compiler inferred type arguments, add second method later on , break existing code without realising it. safer force code explicit.
Comments
Post a Comment