This file contains hidden or 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.Runtime.CompilerServices; | |
// Get, TryFormat, Get, TryFormat の順でログが出る。 | |
Console.WriteLine($"{A.Get(1)}, {A.Get(2)}"); | |
#if false // ↑は↓と同じコードに展開される。 | |
DefaultInterpolatedStringHandler h = new(4, 3); | |
h.AppendFormatted(A.Get(1)); | |
h.AppendLiteral(", "); | |
h.AppendFormatted(A.Get(2)); |
This file contains hidden or 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
元のコード.M(); | |
書き換え後のコード.M(); | |
class 元のコード | |
{ | |
public static void M() | |
{ | |
// x, y | |
for (int x = 1; x < 99; x++) | |
for (int y = 1; y < 99; y++) |
This file contains hidden or 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
Second x = new(1); | |
Console.WriteLine(x); | |
//Console.WriteLine(x.TimeSpan()); // ダメ。 | |
Console.WriteLine(((IIntTime<Second>)x).TimeSpan); // 敗北感強い。box も起こす? JIT 時最適化は効くんだっけ? | |
// たぶん、デフォルト実装じゃなくて、拡張メソッドに変えればそれっぽくはなるかも。 | |
// x.TimeSpan() とか | |
// x.Add(y) とかになる敗北感はまだあるものの… | |
// 根本解決にはたぶん、 |
This file contains hidden or 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 BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using System.Globalization; | |
using System.Runtime.CompilerServices; | |
BenchmarkRunner.Run<StringInterpolationBenchmark>(); | |
[MemoryDiagnoser] | |
public class StringInterpolationBenchmark | |
{ |
This file contains hidden or 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
var r1 = new R1 { X = 1, Y = 2 }; | |
var r2 = new R2(1, 2); | |
Console.WriteLine(r1); | |
Console.WriteLine(r1 with { X = 3 }); | |
Console.WriteLine(r2); | |
Console.WriteLine(r2 with { X = 3 }); | |
public record struct R1 | |
{ |
This file contains hidden or 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.Runtime.InteropServices; | |
namespace GenericMath | |
{ | |
class FixedArraySample | |
{ | |
public static void M() | |
{ | |
// 「generic math」以外でまず真っ先に「固定長配列」をやりたかったんだけど… |
This file contains hidden or 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
// この状態(明示的な ToString オーバーライドなし)は、record のデフォルト動作。 | |
// | |
// R { Name = abc } | |
// D1 { Name = D1 } | |
// D2 { Name = D2 } | |
Console.WriteLine(new R("abc")); | |
Console.WriteLine(new D1()); | |
Console.WriteLine(new D2()); |
This file contains hidden or 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; | |
class Program | |
{ | |
static void Main() | |
{ | |
Action a1 = M; | |
Console.WriteLine(a1.Method.IsStatic); // true | |
Action a2 = static () => { }; |
This file contains hidden or 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
#nullable enable | |
using System.Collections.Generic; | |
// nullability は out 引数でも variance が効く。 | |
string notNull = ""; | |
string? nullable = null; | |
A.In<string?>(notNull); | |
A.In<string?>(nullable); |
This file contains hidden or 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
// https://github.com/dotnet/csharplang/issues/4436 を見てて書きたくなったコード。 | |
// virtual static member の最大用途は generic math。 | |
// numpy とかを使う層を取り込みたいみたい。 | |
// これが来れば0コスト抽象なガロア拡大体とか書けるかもしれないなぁと。 | |
using System; | |
// 整数型の奇数には必ず「逆元」がある。 | |
// 「オーバーフロー前提で掛けて1になる数」がただ1つある。 | |
for (uint n = 3; n != 1; n += 2) |