MIT License

项目介绍

JordanMartinez/purescript-veither 是一个托管在 GitHub 上的开源项目,主要使用 PureScript 编写,采用 MIT License 许可证,目前获得 19 个 Star、3 次 Fork。

仓库全名
JordanMartinez/purescript-veither
开源许可证
MIT License
Star 数
19
Fork 数
3

README 摘要

purescript-veither Why? Because Either forces you to use one error type Either is defined (and often used) like so: However, it's not extensible. The errorType must always be the same. This can be annoying when one is using Either in a monadic way: One way around this is to use a nested Either type (e.g. Either3), but this comes at the cost of extra layers of "boxing:" One could reduce the amount of boxing by using…

完整内容见 GitHub 原始 README 或本页 。

常见问题

purescript-veither 用什么语言开发?

主要使用 PureScript 编写,采用 MIT License 许可证,目前有 19 个 Star。

purescript-veither 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 purescript-veither 的源码?

可以直接克隆仓库:git clone https://github.com/JordanMartinez/purescript-veither.git,默认分支为 master;安装与运行步骤见仓库 README。

purescript-veither 最近还在维护吗?

仓库最近一次更新为 2021年9月11日,当前没有未关闭的 Issue;Star 数为 19,可作为社区活跃度的参考。

关于

A Variant that functions like Either but is extensible

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
25
价值分
12
活跃状态
活跃
暂无主题

语言占比

Dhall
PureScript

Release

README

purescript-veither

Why? Because Either forces you to use one error type

Either is defined (and often used) like so:

data Either errorType valueType
    = Left errorType
    | Right valueType

However, it’s not extensible. The errorType must always be the same. This can be annoying when one is using Either in a monadic way:

foo :: Either SameErrorType
foo = do
  a <- stringOrFailWithErrorType1
  b <- stringOrFailWithErrorType2 -- uh-oh! Compiler error!
  pure $ doSomethingWith a b

One way around this is to use a nested Either type (e.g. `Either3), but this comes at the cost of extra layers of “boxing:”

import Data.Either (Either(..))
import Data.Either.Nested (Either5)
import Data.Either.Inject (inj)

-- Syntactic sugar to help here...
foo :: Either5 a b c d e
foo = inj d

-- but it comes down to this
bar :: Either a (Either b (Either c (Either d e)))
bar = Right (Right (Right (Left d)))

One could reduce the amount of boxing by using Variant. However, Variant does not have a Monad instance, so one cannot use the “do notation” in the original example above.

This library enables one to use all the type classes of Either (except Eq1 and Ord1, which haven’t yet been implemented) but with the extensible error type capaility of Variant.

How? By hard-coding one of the rows in Variant to work like Right and leaving the others to work like Left

What if you took Variant and provided a newtype around it that hard-coded one of the rows to something that functions like Either’s Right constructor (e.g. ("_" :: a)) and then made it extensible, so that the other rows function like an extensible Left constructor (e.g. ("_" :: a | errorRows))? You get Veither, a Variant-based version of Either.

newtype Veither errorRows a = Veither (Variant ("_" :: a | errorRows))
--
-- `=~=` means 'isomorphic to'
--
-- Veither () a =~= a
-- Veither (foo :: Int) a =~= Either Int a
-- Veither (foo :: Int, bar :: String) a =~= Either Int (Either String a)

Now we can write our monadic Either code without the ‘same error type’ restriction:

type PossibleErrors = (a :: ErrorType1, b :: ErrorType2)

foo :: Veither PossibleErrors String
foo = do
  a <- stringOrFailWithErrorType1
  b <- stringOrFailWithErrorType2 -- not a problem!
  pure $ doSomethingWith a b

main :: Effect Unit
main = do
  -- if you would use `either` (or some other
  -- `Either` function) for `Either`, 
  -- add a 'v' in front of it and you have the
  -- corresponding function name for `Veither`
  log $ veither handleFailure identity foo 
  where
  handleFailure :: Variant PossibleErrors -> String
  handleFailure =
    case_
      # on _a (const "error type 1 happened")
      # on _b (const "error type 2 happened")

FAQs

Why use “_” as the label of a?

While it likely doesn’t do much, I thought it would lessen the impact on the runtime checking since checking whether this Variant’s type matches another occurs frequently. I also didn’t want to restrict the possible names one could use in the error types.

评论

暂无评论