Closed ufcpp closed 1 year ago
length-based switch dispatch.
m("aaa");
static int m(string s) => s switch
{
"abc" => 1,
"defg" => 2,
"hijkl" => 3,
"mln" => 4,
"op" => 5,
"qrst" => 6,
"uvw" => 7,
"xy" => 8,
"z" => 9,
_ => 0,
};
using System.Text.Json;
using System.Text.Json.Serialization;
var json = """{ "X": 123, "Y": "abc" }""";
Console.WriteLine(JsonSerializer.Deserialize(json, MyContext.Default.Immutable));
Console.WriteLine(JsonSerializer.Deserialize(json, MyContext.Default.InitProp));
Console.WriteLine(JsonSerializer.Deserialize(json, MyContext.Default.Class));
record class Class(int X, string Y);
readonly partial record struct Immutable(int X, string Y);
readonly partial record struct InitProp
{
public int X { get; init; }
public string Y { get; init; }
}
[JsonSerializable(typeof(Class))]
[JsonSerializable(typeof(Immutable))]
[JsonSerializable(typeof(InitProp))]
partial class MyContext : JsonSerializerContext { }
deserialize 側はリフレクションっぽい雰囲気感じるコード生成だけどどうなんだろ。 InitProp もなんか InitPropCtorParamInit 生成してる。コンストラクターないのに。
https://github.com/dotnet/project-system/blob/main/docs/build-acceleration.md
<Project>
<PropertyGroup>
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
</PropertyGroup>
</Project>
大体1000ペアで破綻。
using System.Collections.Frozen;
using System.Text.Json;
var data = new KeyValuePair<string, int>[]
{
new("one", 1),
new("two", 2),
new("three", 3),
};
var frozen = data.ToFrozenDictionary();
// Serialize はいける。
var json = JsonSerializer.Serialize(frozen);
Console.WriteLine(json);
// 直接の Deserialize<FrozenDictionary<string, int>>() は無理
var x = JsonSerializer.Deserialize<Dictionary<string, int>>("""
{"one":1,"two":2,"three":3}
""")!.ToFrozenDictionary();
using System.Text;
var config = new
{
// 外部から読むときは {x} みたいな interpolation 使えない
MyFooFormat = "{0} = {1}",
};
// いままで or パフォーマンス気にしない
for (int i = 0; i < 1000; i++)
{
_ = string.Format(config.MyFooFormat, i, 2);
}
// これから、パフォーマンスが必須なら
// 最初に1回手間かける
var format = CompositeFormat.Parse(config.MyFooFormat);
for (int i = 0; i < 1000; i++)
{
// その後繰り返し使うなら速い
_ = string.Format(null, format, i, 2);
}
var f = (int x = 1, params int[] y) => Console.WriteLine(x);
A2 f2 = (int x = 1, params int[] y) => Console.WriteLine(x);
f(); // 1
f2(); // 2
Console.WriteLine(f2.Method.GetParameters()[0].DefaultValue); // 1
delegate void A2(int x = 2, params int[] y);
using System.Reflection;
A a = (int x = 2) => { };
MethodInfo m1 = a.Method;
MethodInfo m2 = a.GetType().GetMethod("Invoke")!;
Console.WriteLine(m1.GetParameters()[0].DefaultValue); // 2
Console.WriteLine(m2.GetParameters()[0].DefaultValue); // 1
delegate void A(int x = 1);
配信URL: https://youtube.com/live/Kai58qBvaVA トラッキング用: https://csharp.connpass.com/event/276195/
(2/22 追記)出た。
{
の位置がおかしくてなえ気味。https://developercommunity.visualstudio.com/t/Place-open-brace-settings-are-buggy/10287032?port=1025&fsid=e8019e49-2128-44f2-90ce-a93fc44c9519
C
事前のメモ
core 8133
<WasmEnableWebcil>true</WasmEnableWebcil>