Haskell安全货币示例

问题描述 投票:2回答:1

[这位伟大的作者通过他们写得很好的博客文章完全说服了我使用safe-money

https://ren.zone/articles/safe-money

但是尝试使用GHCi中的示例并尝试各种导入语句,即使重新键入博客文章中的确切示例,我也无法使其正常工作:

Prelude> :m Data.Ratio

Prelude Data.Ratio> 1 % 5
1 % 5

...

Prelude Data.Ratio Data.Text> :m +Money

Prelude Data.Ratio Data.Text Money> :t dense
dense :: Rational -> Maybe (Dense currency)

Prelude Data.Ratio Data.Text Money> let y = 5 :: Integer
Prelude Data.Ratio Data.Text Money> :t y
y :: Integer
Prelude Data.Ratio Data.Text Money> y % 100
1 % 20
Prelude Data.Ratio Data.Text Money> :t it
it :: Ratio Integer
Prelude Data.Ratio Data.Text Money> let z = y % 100
Prelude Data.Ratio Data.Text Money> :t z
z :: Ratio Integer
Prelude Data.Ratio Data.Text Money> dense z

<interactive>:30:1: error:
    • No instance for (GHC.TypeLits.KnownSymbol currency0)
        arising from a use of ‘print’
    • In a stmt of an interactive GHCi command: print it

Prelude Data.Ratio Data.Text Money> fromRational z
5.0e-2
Prelude Data.Ratio Data.Text Money> :t it
it :: Fractional a => a

Prelude Data.Ratio Data.Text Money> fromRational z :: Dense "USD"

<interactive>:35:25: error:
    Illegal type: ‘"USD"’ Perhaps you intended to use DataKinds
Prelude Data.Ratio Data.Text Money> 'fromRational' z :: Dense "USD"

<interactive>:36:1: error:
    • Syntax error on 'fromRational'
      Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes
    • In the Template Haskell quotation 'fromRational'

<interactive>:36:27: error:
    Illegal type: ‘"USD"’ Perhaps you intended to use DataKinds
Prelude Data.Ratio Data.Text Money>  'fromRational' (341 % 100) :: Dense "USD"

<interactive>:37:2: error:
    • Syntax error on 'fromRational'
      Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes
    • In the Template Haskell quotation 'fromRational'

<interactive>:37:38: error:
    Illegal type: ‘"USD"’ Perhaps you intended to use DataKinds

我希望像这样的聪明作者也能切合实际,并为初学者到中级散客提供零基础知识。

* ackage页面和github也是裸露的。有人可以在这里提供使用safe-money的有效示例吗? yesod文档中的完整示例肯定会破坏我。

我相信包裹是这样的:https://hackage.haskell.org/package/safe-money

haskell
1个回答
0
投票

正如luqui的注释,您需要启用DataKinds扩展名。

您可以通过以下方式在文件中启用此扩展名:>

{-# LANGUAGE DataKinds #-}

您可以通过以下方式在GHCi中启用它:>

:set -XDataKinds

GHCi中提供的错误消息应该很好地提示您需要启用该扩展名。

非法类型:“ USD”,也许您打算使用DataKinds

它可以通过在此处浏览搜索结果来帮助您查看更多用法示例:https://github.com/search?q=import+Money+dense+language%3Ahaskell&type=Code

© www.soinside.com 2019 - 2024. All rights reserved.