Replies: 9 comments
-
This seems like it should be considered as a part of #36, not it's own feature. |
Beta Was this translation helpful? Give feedback.
-
static IEnumerable<T> GetNonNullItemList<T>(IEnumerable<T> list) where T : ?
{
return list.Where(x => x != null);
} So, if I pass In my opinion, what you want is to be able to write static IEnumerable<T> GetNonNullItemList<T>(IEnumerable<T?> list)
{
return list.Where(x => x != null).Select(x => (T)x);
} The problem with this is that there is no good way to express it at the IL level. But maybe it could work with some kind of CLR modification. |
Beta Was this translation helpful? Give feedback.
-
In this special case, yes. But if you enter reference types, then it fits. static IEnumerable<T!> GetNonNullitemList<T>(IEnumerable<T> list) where T : ?
{
return list.Where(x => x != null).Select(x => (T!)x);
} Update: // the generic class accepts all nullable types
class Tools<T> where T : ?
{
// no casting on returned value for reference types
public IEnumerable<T> GetNonNullitemList<T>(IEnumerable<T> list) where T : class
=> list.Where(x => x != null);
// casting on retured value for value types
public IEnumerable<T> GetNonNullitemList<T>(IEnumerable<T?> list) where T : struct
=> list.Where(x => x != null).Select(x => (T)x);
} |
Beta Was this translation helpful? Give feedback.
-
Why not just have:
This will work on lists of nullables, and lists of reference types. It will appropriately not work on lists of value types. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi If overload resolution let us have that, it would be amazing. |
Beta Was this translation helpful? Give feedback.
-
Overload resolution should give you that today. I just tested and it worked exactly as expected: static IEnumerable<T> GetNonNullitemList<T>(IEnumerable<T?> list) where T : struct
=> list.Where(x => x != null).Select(x => x.Value);
static IEnumerable<T> GetNonNullitemList<T>(IEnumerable<T> list) where T : class
=> list.Where(x => x != null);
void Test(IEnumerable<int> structs, IEnumerable<int?> nullables, IEnumerable<string> classes)
{
// GetNonNullitemList(structs); <-- get compiler errror.
GetNonNullitemList(nullables);
GetNonNullitemList(classes);
} |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi it will break when it's the return type that is nullable: static T? SafeGet(IReadOnlyList<T> source, int index)
=> (index >= 0 && index < source.Count) ? source[index] : null; |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi static IEnumerable<T> GetNonNullitemList<T>(IEnumerable<T?> list)
=> list.Where(x => x != null).Select(x => (T)x); A possible IEnumerable<T> GetNonNullitemList<T>(IEnumerable<T?> list) where T : struct
IEnumerable<T> GetNonNullitemList<T>(IEnumerable<T> list) where T : class Same goes for // we can safely call a method on x, because x cannot be null
static IEnumerable<Type> GetTypeList<T>(IEnumerable<T!> list) where T : !
=> list.Select(x => x.GetType()); The In case of a CLR change, the |
Beta Was this translation helpful? Give feedback.
-
I know this is old but is there a plan to implement this? I ran into this problem several times lately. Another example: static class DictionaryExtensions
{
public static TValue SafeGet<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TValue : class
{
return dictionary.TryGetValue(key, out TValue value) ? value : null;
}
public static TInnerValue SafeGet<TKey, TInnerKey, TInnerValue>(this Dictionary<TKey, Dictionary<TInnerKey, TInnerValue>> dictionary, TKey key, TInnerKey innerKey) where TInnerValue : class
{
return dictionary.SafeGet(key)?.SafeGet(innerKey);
}
} This works fine for reference types. But when I want to use it for nullable value types it breaks as the first SafeGet uses a dictionary where TValue is a dictionary itself and this is of course a reference type. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
There are sometimes scenarios where a generic class or function is always valid for nullable value types as well as (nullable) reference types.
Currently there is no other way as to duplicate the code
I propose a type constraint
?
(or??
, or???
) that allows for all nullable typesThe "Questionmark"
?
would fit in nicely with the current meanings.This could be extended to allow only non-nullable types and non-nullable reference types
Beta Was this translation helpful? Give feedback.
All reactions