Replies: 8 comments 3 replies
-
I think this also should work: IQuerable<int> M() => Array.Empty().AsQueryable();
public static T[] Empty<T>();
public static IQueryable<T> AsQueryable<T>(this IEnumerable<T>); |
Beta Was this translation helpful? Give feedback.
-
What are your thoughts on providing a (sane) default via an overload? I could see it being useful in some cases, such as where an API developer would like to provide a default return type that can act as a catch-all ( However, I could also see it causing some undesirable or perhaps unexpected behavior depending on implementation, especially if the user assumes no type parameter was needed because it was implied rather than because it fell back to a type-parameterless overload. private readonly Dictionary<string, object> _dict;
public string Find(string key)
=> Find<string>(key);
public TResult Find<TResult>(string key)
{
// Some sort of dummy lookup code
return _dict[key] as TResult;
} int id = item.Find("Id"); // Legal, as discussed by op.
var name = item.Find("name"); // Falls back to type-parameterless overload. Type becomes 'string'. |
Beta Was this translation helpful? Give feedback.
-
@Foxtrek64 that would mean that replacing a type with var is a breaking change. I suspect that makes this feature a non-starter. |
Beta Was this translation helpful? Give feedback.
-
It's not a "breaking change" if you have to modify source to exhibit the different behavior. But I agree that this could be confusing behavior. |
Beta Was this translation helpful? Give feedback.
-
I've been using a Example: public static Result<Word, ParseError> ParseWord(string word)
{
if (string.IsNullOrWhiteSpace(word))
{
return new Err<Word, ParserError>(ParserError.ContainsOnlyWhiteSpace); <--- easily inferable type params
}
...
} instead of public static Result<Word, ParseError> ParseWord(string word)
{
if (string.IsNullOrWhiteSpace(word))
{
return new Err(ParserError.ContainsOnlyWhiteSpace);
}
...
} |
Beta Was this translation helpful? Give feedback.
-
This was raised as a possible solution for #3950. |
Beta Was this translation helpful? Give feedback.
-
I'm chiming in here because I've personally encountered a number of situations where this would be really nice to have (almost all have been a similar situation to the OP, where you must repeatedly access untyped data). Reduced to a minimal example, I just feel like this should work in C#: DateTime dateTime = Parse("2023-01-01");
T Parse<T>(string s) where T : IParsable<T> => T.Parse(s, null); I think it's philosophically consistent with all the other work that's been done lately on target-typing. |
Beta Was this translation helpful? Give feedback.
-
With collection expressions, there's a new interesting case: Func(Array.Empty<SomeLongTypeName>().ToAsyncEnumerable());
Func([].ToAsyncEnumerable()); // Not working now |
Beta Was this translation helpful? Give feedback.
-
Copied from dotnet/roslyn#5429
Problem
When calling generic methods where the generic type parameter is only used as the return value of the method there is currently no inference which leads to repetition.
Proposal
It would be convenient if in the specific case of the generic type parameter being the return type if the compiler could infer the generic type argument based on the destination of the result. This would only be permitted if the method is called in an unambiguous fashion. The return value must be assigned to a destination with an explicit type and there can be no other non-generic methods with the same name and parameters.
Beta Was this translation helpful? Give feedback.
All reactions