参数数据构造函数和 GADT:不在数据构造函数范围内
Parametric data constructor and GADTs : Not in scope data constructor
我有这个核心 Haskell 文件:Relation.hs
它定义了一些数据类型
{-# LANGUAGE GADTs,MultiParamTypeClasses,ScopedTypeVariables,TypeSynonymInstances,FlexibleInstances #-}
module Relation where
data SchemaField schemaField where
SchemaField :: (Eq fieldType) => {
name :: String,
fieldType :: fieldType,
nullable :: Bool
} -> SchemaField (String,fieldType,Bool)
data ValueField valueField where
ValueField :: (Eq valueField,Ord valueField) => valueField -> ValueField valueField
type Schema schemaField = [SchemaField schemaField]
type MaybeValueField valueField = Maybe (ValueField valueField)
type Row valueField = [MaybeValueField valueField]
type Rows valueField = [Row valueField]
data Relation schemaField valueField = Relation {
schema :: Schema schemaField,
rows :: Rows valueField
}
然后,我有测试文件:RelationTest.hs
它需要使用下面的关系构造函数:
{-# LANGUAGE MultiParamTypeClasses #-}
module RelationTest where
import Relation (FromValueToSchema,bindType,schema,rows,Relation)
data FieldType =
StringType | IntType -- Char | Bool | Integer | Float | Double
deriving (Show,Eq)
consistentSchema = [SchemaField "name" StringType False,SchemaField "ID" IntType True]
createdRelation = Relation {schema = consistentSchema, rows = []}
但是当我编译RelationTest.hs时,我有这个错误我不明白:
不在范围内:数据构造函数“Relation”
据了解,关系构造函数在 Relation.hs.
中定义明确
我可以删除 RelationTest.hs 中“import Relation ...”中的详细信息,它工作正常。
但我想保留详细的导入。
删除此导入后,在 GHCi 解释器中,我可以看到 Relation constructor type :
*Main> import Relation
*Main Relation> :t Relation
Relation
:: Schema schemaField
-> Rows valueField -> Relation schemaField valueField
那么,我在详细导入中遗漏了什么?
我检查了那 2 个 Whosebug 线程,但没有找到任何解决方案:
- Haskell: Not in scope: data constructor
- Not in scope: data constructor?
在 RelationTest.hs
文件中,您正在使用以下内容从 Relation
模块导入特定类型的构造函数:
import Relation (FromValueToSchema,bindType,schema,rows,Relation)
关于数据类型,这只导入了类型构造函数。不导入数据构造函数、可能的方法和字段名称。在类型构造函数或 class 的末尾添加 (..)
将导入所有可能的成员。如下:
import Relation (FromValueToSchema,bindType,schema,rows,Relation(..))
Haskell Report 的第 5.3.1 节指出:
Exactly which entities are to be imported can be specified in one of the following three ways:
- The imported entities can be specified explicitly by listing them in
parentheses. Items in the list have the same form as those in export
lists, except qualifiers are not permitted and the `module modid'
entity is not permitted. When the (..) form of import is used for a
type or class, the (..) refers to all of the constructors, methods, or
field names exported from the module.
我有这个核心 Haskell 文件:Relation.hs
它定义了一些数据类型
{-# LANGUAGE GADTs,MultiParamTypeClasses,ScopedTypeVariables,TypeSynonymInstances,FlexibleInstances #-}
module Relation where
data SchemaField schemaField where
SchemaField :: (Eq fieldType) => {
name :: String,
fieldType :: fieldType,
nullable :: Bool
} -> SchemaField (String,fieldType,Bool)
data ValueField valueField where
ValueField :: (Eq valueField,Ord valueField) => valueField -> ValueField valueField
type Schema schemaField = [SchemaField schemaField]
type MaybeValueField valueField = Maybe (ValueField valueField)
type Row valueField = [MaybeValueField valueField]
type Rows valueField = [Row valueField]
data Relation schemaField valueField = Relation {
schema :: Schema schemaField,
rows :: Rows valueField
}
然后,我有测试文件:RelationTest.hs
它需要使用下面的关系构造函数:
{-# LANGUAGE MultiParamTypeClasses #-}
module RelationTest where
import Relation (FromValueToSchema,bindType,schema,rows,Relation)
data FieldType =
StringType | IntType -- Char | Bool | Integer | Float | Double
deriving (Show,Eq)
consistentSchema = [SchemaField "name" StringType False,SchemaField "ID" IntType True]
createdRelation = Relation {schema = consistentSchema, rows = []}
但是当我编译RelationTest.hs时,我有这个错误我不明白:
不在范围内:数据构造函数“Relation”
据了解,关系构造函数在 Relation.hs.
中定义明确
我可以删除 RelationTest.hs 中“import Relation ...”中的详细信息,它工作正常。
但我想保留详细的导入。
删除此导入后,在 GHCi 解释器中,我可以看到 Relation constructor type :
*Main> import Relation
*Main Relation> :t Relation
Relation
:: Schema schemaField
-> Rows valueField -> Relation schemaField valueField
那么,我在详细导入中遗漏了什么?
我检查了那 2 个 Whosebug 线程,但没有找到任何解决方案:
- Haskell: Not in scope: data constructor
- Not in scope: data constructor?
在 RelationTest.hs
文件中,您正在使用以下内容从 Relation
模块导入特定类型的构造函数:
import Relation (FromValueToSchema,bindType,schema,rows,Relation)
关于数据类型,这只导入了类型构造函数。不导入数据构造函数、可能的方法和字段名称。在类型构造函数或 class 的末尾添加 (..)
将导入所有可能的成员。如下:
import Relation (FromValueToSchema,bindType,schema,rows,Relation(..))
Haskell Report 的第 5.3.1 节指出:
Exactly which entities are to be imported can be specified in one of the following three ways:
- The imported entities can be specified explicitly by listing them in parentheses. Items in the list have the same form as those in export lists, except qualifiers are not permitted and the `module modid' entity is not permitted. When the (..) form of import is used for a type or class, the (..) refers to all of the constructors, methods, or field names exported from the module.