Created
June 26, 2019 05:35
-
-
Save ufcpp/f2f5e84276fcf6396b30b9663c013aea to your computer and use it in GitHub Desktop.
Visual Studio 16.2 Preview 3 がらみ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics.CodeAnalysis; | |
// notnull 制約は 16.2.P3 から対応してそう。 | |
// ただ、Visual Studio 上ではコード補完、ハイライト効いてない。 | |
class C<T> | |
where T : notnull | |
{ | |
public T Value { get; } | |
public C(T value) => Value = value; | |
} | |
public class Program | |
{ | |
static void Main() | |
{ | |
var c = new C<string>(""); | |
Console.WriteLine(c.Value.Length); | |
var c1 = new C<int>(1); | |
var c2 = new C<int?>(1); // 警告あり | |
var c3 = new C<string?>(""); // 警告あり | |
var c4 = new C<string>(null); // 警告あり | |
var s = X(""); | |
Console.WriteLine(s.Length); // 警告なくなるはずだけど未対応 | |
if (X(0, out s)) Console.WriteLine(s.Length); // 警告なくなるはずだけど未対応 | |
} | |
// 16.2.P3 マイルストーンに並んでたんだけど対応してなさそう | |
static bool X(int i, [NotNullWhen(true)] out string? s) | |
{ | |
if(i == 0) | |
{ | |
s = ""; | |
return true; | |
} | |
else | |
{ | |
s = null; | |
return false; | |
} | |
} | |
[return: MaybeNull] | |
static T FirstOrDefault<T>(IEnumerable<T> e, Func<T, bool> predicate) | |
{ | |
foreach (var x in e) | |
{ | |
if (predicate(x)) return x; | |
} | |
return default; | |
} | |
// 元から 16.2.P4 マイルストーン | |
[return: NotNullIfNotNull("s")] | |
static string? X(string? s) => s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment