nullable

Can Nullable be used as a functor in C#?

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-22 08:32:33
问题 Consider the following code in C#. public int Foo(int a) { // ... } // in some other method int? x = 0; x = Foo(x); The last line will return a compilation error cannot convert from 'int?' to 'int' which is fair enough. However, for example in Haskell there is Maybe which is a counterpart to Nullable in C#. Since Maybe is a Functor I would be able to apply Foo to x using fmap . Does C# have a similar mechanism? 回答1: We can implement such functionality ourselves: public static class FuncUtils

Can Nullable be used as a functor in C#?

荒凉一梦 提交于 2020-08-22 08:32:10
问题 Consider the following code in C#. public int Foo(int a) { // ... } // in some other method int? x = 0; x = Foo(x); The last line will return a compilation error cannot convert from 'int?' to 'int' which is fair enough. However, for example in Haskell there is Maybe which is a counterpart to Nullable in C#. Since Maybe is a Functor I would be able to apply Foo to x using fmap . Does C# have a similar mechanism? 回答1: We can implement such functionality ourselves: public static class FuncUtils

How to avoid irrelevant nullable warning (without explicit suppression)

扶醉桌前 提交于 2020-07-30 08:08:08
问题 Is there a way to make the analyzer understand that the variable Bar has a value for the following case? #nullable enable class Foo { bool GenerateArray => Bar.HasValue; int? Bar { get; set; } void FooBar() { var data = (GenerateArray) ? new int[Bar.Value] : null; } } There is the warning "Nullable value type may be null." for Bar.Value but it obviously can't be. I am aware of two ways to avoid the warning. Both have disadvantages: Using Bar.HasValue directly instead of the property

Why don't I get a warning about possible dereference of a null in C# 8 with a class member of a struct?

本小妞迷上赌 提交于 2020-07-29 11:38:07
问题 In a C# 8 project with nullable reference types enabled, I have the following code which I think should give me a warning about a possible null dereference, but doesn't: public class ExampleClassMember { public int Value { get; } } public struct ExampleStruct { public ExampleClassMember Member { get; } } public class Program { public static void Main(string[] args) { var instance = new ExampleStruct(); Console.WriteLine(instance.Member.Value); // expected warning here about possible null

Nullable var inside string template

こ雲淡風輕ζ 提交于 2020-06-23 07:05:12
问题 Kotlin has a feature called string templates. Is it safe to use nullable variables inside a string? override fun onMessageReceived(messageEvent: MessageEvent?) { Log.v(TAG, "onMessageReceived: $messageEvent") } Will the above code throw NullPointerException if messageEvent is null ? 回答1: You can always make a tiny project on try.kotlinlang.org and see for yourself: fun main(args: Array<String>) { test(null) } fun test(a: String?) { print("result: $a") } This code compiles fine and prints null

Nullable var inside string template

一个人想着一个人 提交于 2020-06-23 07:05:12
问题 Kotlin has a feature called string templates. Is it safe to use nullable variables inside a string? override fun onMessageReceived(messageEvent: MessageEvent?) { Log.v(TAG, "onMessageReceived: $messageEvent") } Will the above code throw NullPointerException if messageEvent is null ? 回答1: You can always make a tiny project on try.kotlinlang.org and see for yourself: fun main(args: Array<String>) { test(null) } fun test(a: String?) { print("result: $a") } This code compiles fine and prints null

Is there a convenient way to filter a sequence of C# 8.0 nullable references, retaining only non-nulls?

。_饼干妹妹 提交于 2020-06-14 04:08:48
问题 I have code like this: IEnumerable<string?> items = new [] { "test", null, "this" }; var nonNullItems = items.Where(item => item != null); //inferred as IEnumerable<string?> var lengths = nonNullItems.Select(item => item.Length); //nullability warning here Console.WriteLine(lengths.Max()); How can I write this code in a convenient way such that: There is no nullability warning, because the type nonNullItems is inferred as IEnumerable<string> . I don't need to add unchecked non-nullability