前言
有一個東西叫做鴨子類型,所謂鴨子類型就是,只要一個東西表現得像鴨子那麼就能推出這玩意就是鴨子。
c# 裡面其實也暗藏了很多類似鴨子類型的東西,但是很多開發者並不知道,因此也就沒法好好利用這些東西,那麼今天我細數一下這些藏在編譯器中的細節。
不是只有 task 和 valuetask 才能 await
在 c# 中編寫異步代碼的時候,我們經常會選擇將異步代碼包含在一個 task 或者 valuetask 中,這樣調用者就能用 await 的方式實現異步調用。
西卡西,並不是只有 task 和 valuetask 才能 await。task 和 valuetask 背後明明是由線程池參與調度的,可是為什麼 c# 的 async/await 卻被說成是 coroutine 呢?
因為你所 await 的東西不一定是 task/valuetask,在 c# 中只要你的類中包含 getawaiter() 方法和 bool iscompleted 屬性,並且 getawaiter() 返回的東西包含一個 getresult() 方法、一個 bool iscompleted 屬性和實現了 inotifycompletion,那麼這個類的對象就是可以 await 的 。
因此在封裝 i/o 操作的時候,我們可以自行實現一個 awaiter,它基於底層的 epoll/iocp 實現,這樣當 await 的時候就不會創建出任何的線程,也不會出現任何的線程調度,而是直接讓出控制權。而 os 在完成 i/o 調用後通過 completionport (windows) 等通知用戶態完成異步調用,此時恢復上下文繼續執行剩餘邏輯,這其實就是一個真正的 stackless coroutine。
public class MyTask<T>
{
public MyAwaiter<T> GetAwaiter()
{
return new MyAwaiter<T>();
}
}
public class MyAwaiter<T> : INotifyCompletion
{
public bool IsCompleted { get; private set; }
public T GetResult()
{
throw new NotImplementedException();
}
public void OnCompleted(Action continuation)
{
throw new NotImplementedException();
}
}
public class Program
{
static async Task Main(string[] args)
{
var obj = new MyTask<int>();
await obj;
}
}
事實上,.net core 中的 i/o 相關的異步 api 也的確是這麼做的,i/o 操作過程中是不會有任何線程分配等待結果的,都是 coroutine 操作:i/o 操作開始後直接讓出控制權,直到 i/o 操作完畢。而之所以有的時候你發現 await 前後線程變了,那只是因為 task 本身被調度了。
uwp 開發中所用的 iasyncaction/iasyncoperation
不是只有 ienumerable 和 ienumerator 才能被 foreach
經常我們會寫如下的代碼:
foreach (var i in list)
{
// ......
}
然後一問為什麼可以 foreach,大多都會回復因為這個 list 實現了 ienumerable 或者 ienumerator。
但是實際上,如果想要一個對象可被 foreach,只需要提供一個 getenumerator() 方法,並且 getenumerator() 返回的對象包含一個 bool movenext() 方法加一個 current 屬性即可。
class MyEnumerator<T>
{
public T Current { get; private set; }
public bool MoveNext()
{
throw new NotImplementedException();
}
}
class MyEnumerable<T>
{
public MyEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
}
class Program
{
public static void Main()
{
var x = new MyEnumerable<int>();
foreach (var i in x)
{
// ......
}
}
}
不是只有 iasyncenumerable 和 iasyncenumerator 才能被 await foreach
同上,但是這一次要求變了,getenumerator() 和 movenext() 變為 getasyncenumerator() 和 movenextasync()。
其中 movenextasync() 返回的東西應該是一個 awaitable
class MyAsyncEnumerator<T>
{
public T Current { get; private set; }
public MyTask<bool> MoveNextAsync()
{
throw new NotImplementedException();
}
}
class MyAsyncEnumerable<T>
{
public MyAsyncEnumerator<T> GetAsyncEnumerator()
{
throw new NotImplementedException();
}
}
class Program
{
public static async Task Main()
{
var x = new MyAsyncEnumerable<int>();
await foreach (var i in x)
{
// ......
}
}
}
ref struct 要怎麼實現 idisposable
眾所周知 ref struct 因為必須在棧上且不能被裝箱,所以不能實現接口,但是如果你的 ref struct 中有一個 void dispose() 那麼就可以用 using 語法實現對象的自動銷毀。
ref struct MyDisposable
{
public void Dispose() => throw new NotImplementedException();
}
class Program
{
public static void Main()
{
using var y = new MyDisposable();
// ......
}
}
不是只有 range 才能使用切片
c# 8 引入了 ranges,允許切片操作,但是其實並不是必須提供一個接收 range 類型參數的 indexer 才能使用該特性。
只要你的類可以被計數(擁有 length 或 count 屬性),並且可以被切片(擁有一個 slice(int, int) 方法),那麼就可以用該特性。
class MyRange
{
public int Count { get; private set; }
public object Slice(int x, int y) => throw new NotImplementedException();
}
class Program
{
public static void Main()
{
var x = new MyRange();
var y = x[1..];
}
}
不是只有 index 才能使用索引
c# 8 引入了 indexes 用於索引,例如使用 ^1 索引倒數第一個元素,但是其實並不是必須提供一個接收 index 類型參數的 indexer 才能使用該特性。
只要你的類可以被計數(擁有 length 或 count 屬性),並且可以被索引(擁有一個接收 int 參數的索引器),那麼就可以用該特性。
class MyIndex
{
public int Count { get; private set; }
public object this[int index]
{
get => throw new NotImplementedException();
}
}
class Program
{
public static void Main()
{
var x = new MyIndex();
var y = x[^1];
}
}
給類型實現解構
如何給一個類型實現解構呢?其實只需要寫一個名字為 deconstruct() 的方法,並且參數都是 out 的即可。
class MyDeconstruct
{
private int A => 1;
private int B => 2;
public void Deconstruct(out int a, out int b)
{
a = A;
b = B;
}
}
class Program
{
public static void Main()
{
var x = new MyDeconstruct();
var (o, u) = x;
}
}
不是只有實現了 ienumerable 才能用 linq
linq 是 c# 中常用的一種集成查詢語言,允許你這樣寫代碼:
from c in list where c.Id > 5 select c;
但是上述代碼中的 list 的類型不一定非得實現 ienumerable,事實上,只要有對應名字的擴展方法就可以了,比如有了叫做 select 的方法就能用 select,有了叫做 where 的方法就能用 where。
class Just<T> : Maybe<T>
{
private readonly T value;
public Just(T value) { this.value = value; }
public override Maybe<U> Select<U>(Func<T, Maybe<U>> f) => f(value);
public override string ToString() => $"Just {value}";
}
class Nothing<T> : Maybe<T>
{
public override Maybe<U> Select<U>(Func<T, Maybe<U>> _) => new Nothing<U>();
public override string ToString() => "Nothing";
}
abstract class Maybe<T>
{
public abstract Maybe<U> Select<U>(Func<T, Maybe<U>> f);
public Maybe<V> SelectMany<U, V>(Func<T, Maybe<U>> k, Func<T, U, V> s)
=> Select(x => k(x).Select(y => new Just<V>(s(x, y))));
public Maybe<U> Where(Func<Maybe<T>, bool> f) => f(this) ? this : new Nothing<T>();
}
class Program
{
public static void Main()
{
var x = new Just<int>(3);
var y = new Just<int>(7);
var z = new Nothing<int>();
var u = from x0 in x from y0 in y select x0 + y0;
var v = from x0 in x from z0 in z select x0 + z0;
var just = from c in x where true select c;
var nothing = from c in x where false select c;
}
}