點燈坊

失くすものさえない今が強くなるチャンスよ

Fluture 整合 Sanctuary 的 Type Checking

Sam Xiao's Avatar 2021-06-26

由於 Fluture 遵循 Fantasy Land 規格,因此也可搭配 Sanctuary 使用,但由於 Sanctuary 不認識 Future,因此 Type Checking 會失敗,Fluture 已經提供 fluture-sanctuary-types,可使 Sanctuary 認識 Future。

Version

Sanctuary 3.1.0
Fluture 14.0.0

Type Checking Failed

import { resolve, value } from 'fluture'
import { pipe, map, add } from 'sanctuary'
import { log } from 'wink-fp'

let data = resolve (1)

let f = pipe ([
  map (add (1)),
  value (log)
])

f (data) // ?

data 為 Future,當使用 add 透過 map 改變 Future 內部值時,會出現 type checking 錯誤,因為 Sanctury 並不認識 Future。

sanctuary000

Type Checking with Future

import { resolve, value } from 'fluture'
import { env as flutureEnv } from 'fluture-sanctuary-types'
import { create, env } from 'sanctuary'
import { log } from 'wink-fp'

let { pipe, map, add } = create ({ checkTypes: true, env: env.concat (flutureEnv) })

let data = resolve (1)

let f = pipe ([
  map (add (1)),
  value (log)
])

f (data)

第 6 行

let { pipe, map, add } = create ({ checkTypes: true, env: env.concat (flutureEnv) })

Fluture 提供的 fluture-sanctuary-types,讓 Sanctuary 能認識 Future,如此就能通過 type checking。

sanctuary001

Conclusion

  • Fluture 的一大優點是整合 Sanctuary,這使得 Maybe、Either、Future 三大 Monad 都可搭配 Sanctuary 使用

Reference

Fluture, Integration with Sanctuary