使用DataKinds时,无法在GHCI中指定类型签名

时间:2017-01-04 16:54:25

标签: haskell data-kinds

因此,当我尝试使用void Update () { if (active && Input.GetKeyDown (key) && Physics.Raycast (this.transform.position, this.transform.forward, out obj, distance)) { if (obj.collider.gameObject.tag == "Box") { // Get the BoxManager from the object that has been hit box = obj.collider.gameObject.GetComponent<BoxManager>(); box.Open(); Debug.Log("aperto " + box); } } } 时确定多态返回值的类型时,ghci给了我一个有趣的错误。我有以下代码:

DataKinds

此代码按预期编译。如果我在ghci中构造{-# LANGUAGE DataKinds #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE StandaloneDeriving #-} data DataKind = KindA | KindB data SomeData (a :: DataKind) = forall c. SomeData { val :: Int , sub :: Maybe (SomeData c) } deriving instance Show (SomeData a) two :: SomeData 'KindA two = SomeData 2 Nothing 并且没有指定类型它可以正常工作:

SomeData

但如果我尝试指定它出错的类型:

> two
SomeData {val = 2, sub = Nothing}

> :t two
two :: SomeData 'KindA

> SomeData 2 Nothing
SomeData 2 Nothing :: SomeData a

似乎ghci没有解释引用。我使用> SomeData 2 Nothing :: SomeData 'KindA <interactive>:745:32-37: error: • Data constructor ‘KindA’ cannot be used here (Perhaps you intended to use DataKinds) • In the first argument of ‘SomeData’, namely ‘KindA’ In an expression type signature: SomeData KindA In the expression: SomeData 1 Nothing :: SomeData KindA 启动了repl。有没有人遇到过这个?提前感谢您的帮助。

1 个答案:

答案 0 :(得分:9)

SomeData 2 Nothing :: SomeData 'KindA如果您是第一个:seti -XDataKinds,则有效。我的想法是在加载文件时合并了代码文件中的编译指示,但对于在REPL中评估的内容,您还需要明确地在GHCi中启用它们。

我认为这就像在GHCi中一样,你加载的文件更像是导入的模块,REPL中的任何代码都有自己的一组语言扩展。在GHCi中加载多个文件时,您可能不一定希望启用/可用所有加载文件中的所有语言扩展。