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

LDM 話続き& .NET 6 Preview 2 #29

Closed
3 tasks
ufcpp opened this issue Mar 19, 2021 · 32 comments
Closed
3 tasks

LDM 話続き& .NET 6 Preview 2 #29

ufcpp opened this issue Mar 19, 2021 · 32 comments

Comments

@ufcpp
Copy link
Collaborator

ufcpp commented Mar 19, 2021

配信枠: https://youtu.be/NqJkCm85CSM

.NET 6 Preview 2 になってる。

前回残ったやつ:

そこからブログ1記事増えたやつ:

#27 も未消化多い。

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

45:34~ FirstOrNull からの、where 違い拡張メソッドオーバーロード

#nullable enable

using System.Collections.Generic;

_ = new[] { 1 }.FirstOrNull();
_ = new[] { "" }.FirstOrNull();

static class EnumerableClass
{
    public static T? FirstOrNull<T>(this IEnumerable<T> source) where T : class => null;

    // ここにあるとダメ。 .NET の型システム的に where 違いのオーバーロード無理。
    //public static T? FirstOrNull<T>(this IEnumerable<T> source) where T : struct => null;
}

static class EnumerableStruct
{
    // 別の型にあるなら OK。
    public static T? FirstOrNull<T>(this IEnumerable<T> source) where T : struct => null;
}

@ufcpp-live
Copy link
Owner

↑ どうせ↓みたいな分岐ができなくてつむことが多々ある。

image

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

1:00:32~ 引数なしコンストラクターがないことで ImmutableArray の類が困ってる

// 書きたいコード
//struct FixedArray4
//{
//    int[] _array = new int[4];
//    public int this[int index] => _array[index];
//}

// しょうがないから書くコード
struct FixedArray4
{
    int[] _array;

    private FixedArray4(int[] array) => _array = array;

    // これがいやーーーーー
    public static FixedArray4 Create() => new(new int[4]);
    public int this[int index] => _array[index];
}

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

1:03:18~ new()default 扱いされるせいでぬるぽ回避が極めて難しい

using System;
using System.Collections.Immutable;

// 構造体の引数なしコンストラクターがないというか、
//ImmutableArray<int> array = default;

// 今、 new() が default 扱いされてしまい…
ImmutableArray<int> array = new();

// IndexOutOfRange ほしいのに、ぬるぽが来る
// 何を new したんでしょう…
Console.WriteLine(array[0]);

// この判定が必要
Console.WriteLine(array.IsDefault);

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

1:08:35~ 引数なしコンストラクターは C# 6.0 のときに一度やろうとして、Activator.CreateInstance のバグを踏んだことで取りやめになった

using System;

// C# 6.0 の頃、これが構造体コンストラクターを呼んでくれないというバグあった。
var point = Activator.CreateInstance<Point>();

struct Point
{
    public int[] _data;

    // .NET のメタデータ的には昔から許されてた。
    // C# コンパイラーをちょこっといじれば可能だったらしい。
    public Point() => _data = new int[1];
}

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

1:14:59~ (今持っていない状態でリリース済みの)既存の構造体に引数なしコンストラクターを足すのは破壊的変更になります

引数なしコンストラクターを足した瞬間にコンパイルできなくなるコードの例:

void m(Point p = new())
{
}

struct Point
{
    // この行の有無だけで m がエラーになるかどうか変わる。
    // つまり、引数なしコンストラクターの追加は破壊的変更。
    public Point() { }
}

@ufcpp-live
Copy link
Owner

using System;

void m(Point p = new()) // warning waves で警告くらいは出すべきかも
{
}

struct Point
{
}

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

1:28:12~ 今現在、 where T : struct は必ず where T : new() を満たす扱いを受けてる

今後、↑この前提は必ずしも正しくなくなる。
ただ、コンパイル時にエラーにはできなくて、実行時例外が出る状態になるかも。

using System;

m2<Point>();

// 今、「struct の時点で new() を満たす」という判定を受けてる。
void m1<T>() where T : new()
{
    // new T() は中で Activator.CreateInstance<T>() を呼んでる
    Console.WriteLine(new T());
}

// 今、これ許される。
// さすがに変えれない。
void m2<T>() where T : struct => m1<T>();

struct Point
{
    // これを足すと破壊。
    // new T() のところで MissingMethodException が出る。
    private Point() { }
}

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

1:37:00~ .NET って「0バイト構造体」が実はなかったりする(現時点では)

using System;

unsafe
{
    Console.WriteLine(sizeof(Empty)); // これ1
}

struct Empty { }

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

1:39:44 にしてやっとサムネ回収。

var var = new var();
class var { }

@record record = new record();
record record { }

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

1:41:18~ Visual Studio 上だといいけども…

Roslyn みたいにsemantics見まくってるものと同水準のシンタックスハイライト、普通は期待できない。
実際、GitHub 上のハイライトはちょっと変。

image

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

1:44:10~ semantics 見なくてもいい文法になればちょっと GitHub とかブログサービスとかに優しくなれる

// これ、破壊的変更するかも。
// 今後、semantics 見なくても「var はここで出てきたらキーワード」扱いしたい
var var = new var();

// record はそうだから。
// そして C# 9.0 がリリースされて、それで困った人もいない。
record record = new record();

// @ を付ければいいじゃない。
//@record record = new record();

class var { }
record record { }

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

2:02:31~ global using の話
Razor (cshtml) は元から暗黙的に誰かがどこかで using System; してる。

2:12:40~ global using と関連して文脈キーワードの話に戻って
「global は :: の前でだけ文脈キーワード」みたいな扱い。
そして、goto ラベルとかいう文法があったことを思い出す流れ:

image

2:14:40~ global using に関していうと「# ディレクティブとコメントを除いて、他のどのステートメントよりも前」っていう制限があるので文脈を読むのは楽

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

2:16:55~ global using の扱いは「すべてのファイルの先頭にその using をべた展開するのと同じ扱い」になる

2:17:13~「通常 using よりも上」みたいな新しいスコープは導入しない

global using System;
using System;

// これ OK
using D = System.DateTime;

// これダメ
// global using でもダメということにするらしい。
// global using はべたに「すべてのファイルの先頭にその using を展開」扱い。
using D1 = DateTime;
;

namespace A
{
    // これは OK
    using D = DateTime;
}

@ufcpp-live
Copy link
Owner

ufcpp-live commented Mar 20, 2021

2:06:18 sharplab 仕事はえぇ (global using の feature ブランチをもう試せる)

https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQHEA2B7YVMCqyAlgHYDmAIsUgMYzEBuEAPgMSlyaZTCYQACCKV78AsAChJAemkCoCqJMnkceTAIACAJgCMyiTv0SgA==

@ufcpp-live
Copy link
Owner

using System;

var x = 1;

// エイプリルフールその1、オタマジャクシ演算子
Console.WriteLine(~-x);
Console.WriteLine(-~x);

@ufcpp-live
Copy link
Owner

「 60進数リテラル」「エイプリルフール」

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 23, 2021

2:34~ 開幕「きのこ」「たけのこ」「どうでもいい」「そっとしておこう…」

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 23, 2021

8:36~ .NET 6 Preview 2 「開発ループの改善をしたい」
17:18~ MAUI、シングルプロジェクトに
21:48~ MAUI も開始地点、IAppHostBuilder CreateBuilder

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 23, 2021

28:30~ PriorityQueue

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 23, 2021

41:55~ SignalR に nullable アノテーション付いたらしい → 制約なし T? とか MemberNotNull がないとやっぱきついよねみたいな話

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 23, 2021

45:34~ FirstOrNull からの、where 違い拡張メソッドオーバーロード
#29 (comment)
#29 (comment)

Repository owner deleted a comment from ufcpp-live Mar 23, 2021
@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 23, 2021

54:22~ NullableContext 属性の話

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 24, 2021

59:40~ 構造体の引数なしコンストラクター

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 24, 2021

1:22:30~ C##

平均律だと D = C## だけど、純正率だと厳密には違う音なんだっけ?

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 24, 2021

1:27:20~ C# 1.0 でジェネリクスなかったから3年くらいは悲しかった

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 24, 2021

1:46:43~ class は(文脈抜きで)単純にキーワードなので C# だと @ 必須
からの、「他の言語で klass とか clazz とかよく見た」話。

1:48:10 「cls とか略すと clear screen? ってなる」「C# script には #cls ディレクティブで実際 clear screen する文法がある」
1:50:50~ 「もういっそ ß 使えば?ドイツ語のエスツェット。s 2つの合字。 claß」
(ちなみに、Microsoft IME だと「ss」って打って変換で ß が出る)

1:51:46~ 文字コードさえ覚えれいれば何の文字でも F5 キーで入力可能
それが後々「なんとかしてくれるゼロ幅スペース」の話に。

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 24, 2021

1:55:52~ ZWJ (U+200D)を使って複数の文字を1つの絵文字に結合するえぐい仕様の話
1:57:36~ 肌色選択は逆に ZWJ を挟まなくていい唯一の例外なので、逆に判定が面倒なっててほんとやめてほしい
1:58:35~ 出せない ZWJ シーケンスは ZWJ を除いたときと同じ「複数の絵文字を出す」というのが一般的
2:00:18~ Twitter 上、「2文字 + ZWJ」な絵文字シーケンスは「2.5文字扱い」だった

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 24, 2021

2:24:57 「global using 使いたいと思わない」とかいうけど、ソースコード新規追加したときのデフォルトの using うざいじゃない
特に System.Text。単独で使いたいのは StringBuilder しかいない。
(Encoding とかはだいたい System.IO とセット。)
(RegexSystem.Text.RegularExpressions 名前空間。)
実際、「StringBuilderSystem 名前空間にあるべきだった」って言われてる(中の人が反省してるの見たことある)。
(StringInfo とか UnicodeCategory とかは System.Globalization 名前空間だし。)

今(.NET 5 以降)は System.Text.Rune があるけども。

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 24, 2021

2:31:33~ Unicode の仕様がらみの話に

2:36:19~ 絵文字、第1段階として「ZWJ でつながったシーケンスの判定」みたいなやつは ICU に頼れば自分で書く必要なかったりするものの…
最近、Windows にも ICU が標準で入っています
Linux は元々 ICU 依存です
Android も Java と一緒に ICU4J が入っています
iOS で困る… どうせ Safari で ICU 使ってるくせに…

2:37:56~ 絵文字のもう1個厄介な点、絵に著作権がある
オープンライセンスになってて自由に使える絵柄はあるけど、無ライセンスではない。

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 24, 2021

2:45:42 この週末は YouTube の調子ほんと悪かった
「YouTube くんさ~」

@ufcpp
Copy link
Collaborator Author

ufcpp commented Mar 24, 2021

2:48:40~ エイプリルフールネタはやらないというか、「真に受けられて困る嘘はつくな」話

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

No branches or pull requests

2 participants