ufcpp-live / UfcppLiveAgenda

@ufcpp live streaming agenda
MIT License
24 stars 2 forks source link

C# 10.0 (先にC# 9.0の記事を書け) 配信 #16

Closed ufcpp closed 3 years ago

ufcpp commented 3 years ago

.NET 5.0 は RC 2 になりました

一方で、(.NET 5と同時にリリースされる) C# 9.0 に対してこの時期に新機能が入るわけはなく、C# チームの仕事はすでに来年の C# 10.0 向けのものに。 3回に分けてブログ化。これをネタに配信します。

C# 10.0 の話と言いつつ、ブログに対する反応を見るに、「その前提として 9.0 ではここまでを実装していて…」の話の方が食いつきがよさそうな雰囲気で…

ということで、C# 9.0 だか C# 10.0 だかわからない感じの話になるかと。

ブログも思った以上に長くなった結果として3分割になったので、配信でも2時間で話切れなかったら回を改めるかも。

あと、触れたい話題:

ufcpp-live commented 3 years ago

本日のかき捨てコード1

邪悪なやつ。

global::record x = new();
record record { }

「global は冗長だから消すかい?」リファクタリングが出てるけど、消すとコンパイルエラー。

ufcpp-live commented 3 years ago

最初からあったコードなものの補足 ↓ これ、書けるようになった(ちょっと前までコンパイルエラー起こしてた)

record Base(int X);
record Derived() : Base(1);
ufcpp-live commented 3 years ago

やっと本題(配信から1時間以上経過)

C# 10.0 で required property ってのが入りそう

#nullable enable

Console.WriteLine(new C { X = "" }); // ここの X の初期化を必須にしたい

class C
{
    public string X { get; init; } // オブジェクト初期化子での初期化を必須にしないと nullable warning 消せない
    public int Y { get; init; }

    public override string ToString() => (X, Y).ToString();
}

候補としては、required 修飾子の追加とか、init キーワードの代わりにさらに req キーワードみたいなのを足すとか。

ufcpp-live commented 3 years ago

構造体に対する with の話

var x = new PointS(1, 2);
var y = x; // 構造体なのでコピー
y.X = 3;

Console.WriteLine(x);
Console.WriteLine(y);

var x1 = new Point(1, 2);
var y1 = x1 with { X = 3 }; // 構造体でも with 使えてよくない? 10.0 予定。

Console.WriteLine(x1);
Console.WriteLine(y1);

record Point(int X, int Y);

struct PointS
{
    public int X;
    public int Y;

    public PointS(int x, int y)
    {
        X = x;
        Y = y;
    }

    public override string ToString() => (X, Y).ToString();
}
ufcpp-live commented 3 years ago

record の値型版はどうする? 現状、record struct が筆頭候補

そして非常に長くなる型宣言…

// デフォルトで mutable (タプルがそうだから&mutable でも問題起こさないから)
record struct 案1(int X, int Y); // この案が優勢
struct record 案2(int X, int Y);

// record とは record class の短縮系である
record class 案1のおまけ(int X, int Y); // この案も採用されそう

// immutable な struct な record
public readonly record struct ImmutableValueRecord;
public unsafe readonly record ref partial struct 原理上これも;

// まあ、実は真に immutable じゃない。shallow な readonly 性

class Outer
{
    private protected unsafe readonly record ref partial struct 原理上これも;
}

おまけでJavaの場合…

// Java にも record が入る
class C { }
record R { }

// 今、Java で、構造体的なものも検討中
// Java の場合はもともと struct がなく、頭に「value 修飾子」を付けそう。
// これはこれで、DDD 方面の「value object」と混同されてやばそう。
value class C { }
value record R { }
ufcpp-live commented 3 years ago

clone の闇。

using System.Diagnostics.CodeAnalysis;

interface IDeepCloneable
{
    object Clone();
}
interface IDeepCloneable<T> : IDeepCloneable
{
    [return: NotNull]
    new T Clone();
    object IDeepCloneable.Clone() => Clone();
}
ufcpp-live commented 3 years ago

https://youtu.be/aDXHl3S8oik