Does C# have an equivalent to Scala's structural typing?

后端 未结 4 482
小鲜肉
小鲜肉 2020-12-20 11:30

In Scala, I can define structural types as follows:

type Pressable = { def press(): Unit }

This means that I can define a function or method whi

4条回答
  •  情歌与酒
    2020-12-20 12:05

    The awaitable pattern in C# can perhaps be interpreted as a limited, ad hoc instance of structural subtyping / existential typing. The compiler will only await objects that have access to a GetAwaiter() method that returns any INotifyCompletion object with a specific set of methods and properties. Since neither the 'awaitable' object nor the 'awaiter' object needs to implement any interface (except INotifyCompletion in the case of the latter), await is similar to a method that accepts structurally typed awaitable objects.

提交回复
热议问题