如何評價王垠的《Sum types and union types》?

時間 2021-05-14 18:24:10

1樓:slaveoftime

之所以喜歡用F#(ML系語言),有乙個原因就是union type啊(當然還有很多其他原因)。

type

SupportedPayment1=|

CreditCard

ofstring

|Cash

|Wechat

ofstring

[]

// 可選,目的解釋如下

type

SupportedPayment2=|

CreditCard

ofNoneEmptyString

|Cash

|Alipay

ofNoneEmptyString

如果SupportedPayment1 和 SupportedPayment2 放在同乙個模組下,或者同時要使用,那麼CreditCard和Cash自然需要區分,使用的時候就是 SupportedPayment1.CreditCard就可以了。或者可以加乙個 RequiredQualifiedAccess 來強制必須要加 SupportedPayment1 作為字首。

2樓:賀師俊

作為只會JS/TS這種大眾(外行設計的)語言的我,來用JS/TS(強行)解釋一下。

union type 就是 let x: number | string,tagged union type(sum type)就是 let x: | 。那用起來的差別就是:

function

dealWithUnionType(x

)function

dealWithTaggedUnionType

()所以看上去確實沒啥大差別。但是為啥JS在很多場合用的是tagged union呢?

(因為JS是外行設計給外外行用的——劃掉)

因為很多場景下值的型別不是mutual exclusive(互斥)的。比如在JS/TS裡沒法用 float | int32 —— JS 只有乙個 number 型別。更何況有時候你就是需要 | ,也就是兩個value壓根就是一種型別的情況。

所以顯然,(從外行的我的角度看),tagged union才是更general和flexible的形式。

特別的,垠神批評的「a constructor can only belong to one sum type」的問題在JS裡是壓根不存在的,因為JS的這個tagged union根本就只是拿record的形式搞的,用的是structual type。所以他講的這個問題按這個角度說純粹就是具體語言裡用structural type還是nominal type的選擇問題。像我這種天天只能用structural type的人,很想要nominal type的心情,垠神大概是不了解的……

3樓:

這和 sum type 沒關係,他就是想藉口表達不爽 ADT

因為他根本就沒有哪怕一點點認真寫過 Haskell,下面所謂「Sum Type 的痛點」怕不是他癔想出來的。

data T1 = Foo Int | Bar String

Here Foo and Bar are essentially injections. Why do we need the injections and projections in a language where type tags always tell us what an object is? Another problem is that a constructor can only belong to one sum type, and there is no way we can reuse it in another.

For example, there is no way you can define another sum type like:

data T2 = Foo Bool | Baz Float

那啥,真正的 sum type 是 data Either a b = Left a | Right b 了解一下

typeT1=

Either

IntString

typeT2=

Either

Bool

Float

a1::

Either

Boolba1

=Left

False

a2::

Either

aStringa2=

Right

"fuc* k"

a3::

Either

Intba3=

Left1b1

::[Either

Bool

String]b1

=[a1,

a2]b2::[T1

]b2=[

a3,a2]

而且暴露了他全然不懂 CH 同構,當然我覺的可能是他看的很多 Haskell 的屑文章瞎幾把 Sum Type 和 ADT 的 variants/alternation/tagged union 搞混了的原因。

當然我估計他還不知道 tagged union 的叫法,不然為啥還會和 union type 比較。

本來還想著我可以提下 kind-indexed generic function 和 Dynamic Pattern Match 結果然沒給機會麻

如何評價王垠的 A 計畫?

kevin chen 這種說話的語氣不像對技術有敬畏之心的人,而且他的目標已經實現了,mysql不就是?而且他說的東西感覺像江湖郎中那包治百病的藥。 乾貨滿滿張雜湊 一直覺得垠神是所有技術大牛中最會營銷的,自身的知名度本來就很高,先提乙個巨集偉計畫提高知名度後做,本來就是一件穩賺不虧的事 希望盡早能...

如何評價王垠無家可歸的現狀?

不夠強大的理想主義者,很難在這個社會存活,或是墮落成現實主義者,或是離開。然而在真正的理想主義者眼中,足夠 這個詞是不存在的。或許這就是悲哀之處吧。 關鍵是暴露了這種心態求職,有多少公司敢要這種穩定性不高的人,哪怕是大牛。挺殘酷的。一直看他的部落格,大約也意料到這類事情會發生。國內屌絲只能祝好 王垠...

如何評價王垠的 《討厭的 C IDisposable 介面》?

florent 看他的部落格很解壓啊,看完了之後感覺就是同事是sb,領導包庇sb。各種語言都沒用,scheme搞定一切,但是搞定一切的本身就會造成複雜,所以任何概念都是扯淡。你們這些普通人不能理解我這樣人看到問題時候的痛心疾首。大師就是一眼看透本質,然後丟出一句別人全是瞎搞。懟天懟地懟空氣。別問我創...