- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何为 Typeable 编写实际的实例声明?例如,假设我想为 Char 编写一个实例,其中每个字符都有不同的类型,例如
instance Typeable Char where
typeOf 'a' = C
typeOf 'b' = C -> D
typeOf 'c' = D -> E
typeOf = String
最佳答案
好吧,这样做有一些问题。
typeOf
它的论点不应该是严格的,所以typeOf (undefined :: Char)
应该管用。 Typeable
的实例你不会在 Safe Haskell 下编译。 {-# LANGUAGE DeriveDataTypeable #-}
import Data.Typeable
data C = C deriving (Typeable)
data D = D deriving (Typeable)
data E = E deriving (Typeable)
newtype Foo = Foo Char
instance Typeable Foo where
typeOf (Foo 'a') = typeOf (undefined :: C)
typeOf (Foo 'b') = typeOf (undefined :: C -> D)
typeOf (Foo 'c') = typeOf (undefined :: D -> E)
typeOf _ = typeOf (undefined :: String)
what :: Char -> C
what c = if isJust weird then fromJust weird else error "Sanity!"
where weird = fromDynamic . toDyn $ Foo c
> what 'a'
C
> what 'b'
*** Exception: Sanity!
Foo
的表示方式这可以做各种有趣的事情,比如 seg 错误、胡说八道的答案,或者让猴子从你的耳朵里飞出来。
{-# LANGUAGE DeriveDataTypeable #-}
import Control.Exception
import Data.Typeable
newtype Foo = Foo (() -> IO ())
{- set Foo’s TypeRep to be the same as ErrorCall’s -}
instance Typeable Foo where
typeOf _ = typeOf (undefined :: ErrorCall)
instance Show Foo where show _ = ""
instance Exception Foo
main = Control.Exception.catch (error "kaboom") (\ (Foo f) -> f ())
<interactive>: internal error: stg_ap_v_ret
(GHC version 7.6.3 for x86_64_unknown_linux)
Please report this as a GHC bug: http://www.haskell.org/ghc/reportabug
C-c C-c
Process haskell aborted (core dumped)
关于haskell - 为 Typeable 编写实例声明而不进行派生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20896098/
假设我有以下代码: {-# LANGUAGE GADTs, DeriveDataTypeable, StandaloneDeriving #-} import Data.Typeable class
我正在尝试为关联的数据系列派生Typeable,如下所示: {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE TypeFamilies #-} modu
如何为 Typeable 编写实际的实例声明?例如,假设我想为 Char 编写一个实例,其中每个字符都有不同的类型,例如 instance Typeable Char where typeOf
[...] wants to find all the places in a legacy codebase that a variable foo is used in the condition
我大致想要实现以下目标: f :: a -> a g :: b -> b h :: Typeable t => t -> Maybe t h x = case x of (x is of type
例如,假设我们有以下数据结构: data Foo = Bool Bool | Int Int | Double Double 现在,有没有更简单的方法来做到这一点: foo :: Typeable a
所以这是我的对象系统传奇的延续(part 1,part 2)。 这部分基本上归结为以下内容。 {-# LANGUAGE ExistentialQuantification #-} {-# LANGUA
通用编程时间! 如果我有一个函数: f :: a1 -> a2 -> a3 -> ... -> an 和一个值 v :: aX -- where 1 a2 -> a3 -> a4 -> a5)
自 GHC 7.8 起,Typeable 是多类的。查看内置 Typeable 实例列表 in the documentation ,我注意到一些有趣的事情: Typeable ((* -> *) -
我遇到过对 Haskell 的 Data.Typeable 的引用,但我不清楚为什么要在代码中使用它。 它解决了什么问题以及如何解决? 最佳答案 Data.Typeable 是一种众所周知的方法(参见
我正在玩 Haskell 的 Data和 Typeable ,并且我一直试图在上下文中没有可用类型变量的情况下获取函数的参数。 让我澄清一下我的意思。只要我像下面这样对类型变量 a 进行量化,我就可以
有人能给我指出一组在 Haskell 中为 GADT 定义 Typeable 或 Typeable1 实例的好示例吗? 或者,有人可以向我展示如何为以下 GADT 定义 Typeable(手动)。 d
我有一些这样的类型: data Currency = USD | EUR deriving (Show, Typeable) data Money :: Currency
我正在尝试为 (':) 派生一个 Typeable 实例 我正在使用 DataKinds 和 [*]。 我启用了 TypeOperators、StandaloneDeriving 和 PolyKind
我有 data D t = ... data SomeStuff = forall a (Typeable a, ...) => SomeStuff a 在某些时候,我得到了一个 SomeStuff,
我试图使用 Shapeless Typeable 来使对 Java 库的访问更加类型安全,但遇到了障碍。我不确定我是否滥用了该库,是否必须手动提供类型类实例,或者是否有其他问题。 此代码演示了我的问题
我是一名优秀的程序员,十分优秀!