首页 > 解决方案 > 这个纯脚本构造函数有什么问题?

问题描述

尝试为罗马数字创建构造函数:

data RomanDigit a = M | D | C | L | V | I

newRomanDigit :: Int -> RomanDigit 
newRomanDigit 1000 = M 
newRomanDigit 500 = D

得到错误信息:

in module UserMod
at src\UserMod.purs line 81, column 1 - line 81, column 35


  Could not match kind

    Type

  with kind

    Type -> Type


while checking the kind of Int -> RomanDigit
in value declaration newRomanDigit

我究竟做错了什么?

标签: purescript

解决方案


您提供了RomanDigit一个类型参数a,但没有在 的声明中为它指定一个值newRomanDigit

您声明它的方式RomanDigit不是Type. RomanDigit Int是 a Type,或者RomanDigit String是 a Type,或者也许RomanDigit (Array Boolean)是 a Type,但RomanDigit它本身不是 a Type,因为它缺少声明的类型参数a。这就是编译器告诉你的。

您需要删除参数,例如:

data RomanDigit = M | D | C | L | V | I

或者在使用时指定它RomanDigit,例如:

newRomanDigit :: Int -> RomanDigit Int

由于该参数不存在于任何值中,因此我怀疑您并不是真的打算将它放在那里。


推荐阅读