Skip to content

Instantly share code, notes, and snippets.

View ufcpp's full-sized avatar

Nobuyuki Iwanaga ufcpp

View GitHub Profile
@ufcpp
ufcpp / InterpolatedString.cs
Created November 17, 2021 01:28
C# 10 interpolated string が呼ぶメソッドと順序
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));
@ufcpp
ufcpp / ConstIntArray.cs
Created September 1, 2021 15:29
定数配列の類のミス
元のコード.M();
書き換え後のコード.M();
class 元のコード
{
public static void M()
{
// x, y
for (int x = 1; x < 99; x++)
for (int y = 1; y < 99; y++)
@ufcpp
ufcpp / Time.cs
Created August 20, 2021 15:26
generic math (C# 10.0 時点で LangVersion preview 必須) で敗北中
Second x = new(1);
Console.WriteLine(x);
//Console.WriteLine(x.TimeSpan()); // ダメ。
Console.WriteLine(((IIntTime<Second>)x).TimeSpan); // 敗北感強い。box も起こす? JIT 時最適化は効くんだっけ?
// たぶん、デフォルト実装じゃなくて、拡張メソッドに変えればそれっぽくはなるかも。
// x.TimeSpan() とか
// x.Add(y) とかになる敗北感はまだあるものの…
// 根本解決にはたぶん、
@ufcpp
ufcpp / Benchmark.cs
Last active August 14, 2021 09:38
Improved Interpolated Strings (C# 10.0 新機能。 .NET 6 Preview 7/VS 2020 Preview 3以降で有効)
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Globalization;
using System.Runtime.CompilerServices;
BenchmarkRunner.Run<StringInterpolationBenchmark>();
[MemoryDiagnoser]
public class StringInterpolationBenchmark
{
@ufcpp
ufcpp / RecordStructWithField.cs
Created August 12, 2021 07:05
record structをフィールドベースにしたいとき
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
{
@ufcpp
ufcpp / FixedArray.cs
Created July 16, 2021 11:55
fixed-size array
using System;
using System.Runtime.InteropServices;
namespace GenericMath
{
class FixedArraySample
{
public static void M()
{
// 「generic math」以外でまず真っ先に「固定長配列」をやりたかったんだけど…
@ufcpp
ufcpp / A.cs
Created May 14, 2021 13:42
Records with sealed base ToString override #4174
// この状態(明示的な ToString オーバーライドなし)は、record のデフォルト動作。
//
// R { Name = abc }
// D1 { Name = D1 }
// D2 { Name = D2 }
Console.WriteLine(new R("abc"));
Console.WriteLine(new D1());
Console.WriteLine(new D2());
@ufcpp
ufcpp / StaticMethod.cs
Created May 10, 2021 02:27
static っぽく見えるけどインスタンスメソッドが生成されることがあるやつ
using System;
class Program
{
static void Main()
{
Action a1 = M;
Console.WriteLine(a1.Method.IsStatic); // true
Action a2 = static () => { };
@ufcpp
ufcpp / Variance.cs
Created April 16, 2021 08:49
covariance の解析、null 許容参照型の方がジェネリクスよりも賢い
#nullable enable
using System.Collections.Generic;
// nullability は out 引数でも variance が効く。
string notNull = "";
string? nullable = null;
A.In<string?>(notNull);
A.In<string?>(nullable);
@ufcpp
ufcpp / Inverse.cs
Created April 1, 2021 16:33
掛けて1になる整数
// 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)