Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

データの初期化 #308

Closed
ufcpp opened this issue Jul 4, 2020 · 6 comments
Closed

データの初期化 #308

ufcpp opened this issue Jul 4, 2020 · 6 comments
Labels

Comments

@ufcpp
Copy link
Owner

ufcpp commented Jul 4, 2020

レコード型の話は #308 (comment) の話以外は https://ufcpp.net/study/csharp/datatype/record/ に書いたのでタイトル変更。

@ufcpp ufcpp added the C# 9.0 label Jul 4, 2020
@ufcpp
Copy link
Owner Author

ufcpp commented Jul 21, 2020

とりあえず、テーマ的には「making immutable coding easier」。
「C# 9.0 の目玉機能」もこの説明でいいかも。

@ufcpp
Copy link
Owner Author

ufcpp commented Oct 19, 2020

  • 先に、「データの初期化」みたいなページ作った方がいいかも

候補は

候補割れてる理由:

  • record でも struct でも成り立つ話だし「複合型」の辺りにあった方がいい感じがしつつ
  • コンストラクターでは「実装の隠ぺいのために中身を隠しつつ初期化」みたいな OOP 的要素もありつつ
  • new (1, 2)new { X = 1, Y = 2 } とがあるって話がテーマになるので「データ型」のところがぴったりなんだけど、それだと登場位置が遅すぎる感じが

「データの初期化」に書くこと:

  • 歴史的経緯で元々は new T(1, 2) な書き方だった
  • C# 3.0 で new T { X = 1, Y = 2} が書けるように(オブジェクト初期化子)
    • ただ、後付けなのもあって、これは var t = new T(); t.X = 1; t.Y =2; に変換される
    • この実装上の制約で、public set 必須だった
  • その後、working with data をテーマにした時に以下のように概念整理された
    • new T(1, 2) は引数の順序に意味があって、この意味で位置による(positional)初期化
    • new T { X = 1, Y = 2} はプロパティ名に意味があって、この意味で名前による(nominal)初期化
  • 匿名の複合型との関連
    • タプルは positional の方の無名化 new T(1, 2) ⇔ '(1, 2)`
    • 匿名型は nominal の方の無名化 new T { X = 1, Y = 2} ⇔ 'new { X = 1, Y = 2}`
  • init-only
    • 後付け・実装上の都合で非対称になってた positional と nominal の差の補正
    • nominal の方でも immutable データ型を作れるように
    • set の代わりに init と書くと、「オブジェクト初期化子」までは書き換えできるけどそれ以降は書き換え不能になる
    • 補足で 新機能の実装方法(modreq + RuntimeFeature) #295 にリンク
  • target-typed new (ターゲットからの型推論)との兼ね合い
    • positional の方は new (1, 2) と書けるようになった
    • nominal の方、同じ書き方をしようとすると匿名型になっちゃう
      • T t = new { X = 1, Y = 2 }; は、「匿名型から T に変換できません」エラー
      • これを、target があるときは型推論にして、target が var, object, dynamic の時だけ匿名型扱いしようという提案あり

@ufcpp

This comment has been minimized.

@ufcpp

This comment has been minimized.

@ufcpp

This comment has been minimized.

@ufcpp ufcpp changed the title record データの初期化 May 3, 2021
@ufcpp
Copy link
Owner Author

ufcpp commented Sep 22, 2022

もう、#401https://ufcpp.net/study/csharp/oo_property.html?p=2#required を持って close にしちゃう。

@ufcpp ufcpp closed this as completed Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant