为什么我的测试文件不导入我的数据结构? Haskell

Why won't my test file import my datastructure? Haskell

我目前正在为我的(非常简单的)二十一点游戏编写单元测试,我的测试文件 (Tests.hs) 似乎没有导入我在文件中声明的数据结构,我正在为 ( HelpFunctions.hs)。我可以访问此文件中的 functions/methods 但不能访问数据结构。 有人可以帮我找到问题吗?

这是我的测试文件的顶部:

module Tests(performTests) where
import Test.HUnit
import HelpFunctions

cardList = [(Hearts, Ace)]
(...)

这是我要为其编写测试的文件的顶部

module HelpFunctions(Suit, Value, blackjack, cardDeck, shuffleOne, 
                     shuffleCards, getValue, addHand, dealCard, bust, 
                     getHighest
                    ) where

import System.Random
import Control.Monad(when)

{- Suit is one of the four suits or color of a playing card 
   ie Hearts, Clubs, Diamonds or Spades
   INVARIANT: Must be one of the specified.
 -}
data Suit = Hearts | Clubs | Diamonds | Spades deriving (Show)

{- Value is the numeric value of a playing card according to the rules of blackjack. 
   INVARIANT: Must be one of the specified.
 -}
data Value = Two | Three | Four | Five | Six | Seven | Eight | 
             Nine | Ten | Jack | Queen | King | Ace 
             deriving (Eq, Show)
(...)

编译测试文件时出现错误

    Tests.hs:6:14: error: Data constructor not in scope: Hearts
  |
6 | cardList = [(Hearts, Ace)]   |              ^^^^^^

Tests.hs:6:22: error: Data constructor not in scope: Ace
  |
6 | cardList = [(Hearts, Ace)]   |                      ^^^

我有另一个文件从中导入 HelpFunctions 和数据结构,并且可以正常工作。

你的问题在这里:

module HelpFunctions(Suit, Value, ...

这一行说 HelpFunctions 导出类型 SuitValue,但不导出它们的数据构造函数(即类型是抽象的)。

你想要

module HelpFunctions(Suit(..), Value(..), ...

您可以显式列出所有构造函数,但 .. shorthand 表示法表示 "all data constructors of this type".


参考:The Haskell 2010 Language Report, 5.2 Export Lists

  1. An algebraic datatype T declared by a data or newtype declaration may be named in one of three ways:

    • The form T names the type but not the constructors or field names. The ability to export a type without its constructors allows the construction of abstract datatypes (see Section 5.8).
    • The form T(c1,…,cn), names the type and some or all of its constructors and field names.
    • The abbreviated form T(..) names the type and all its constructors and field names that are currently in scope (whether qualified or not).