作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试使用 HList创建记录。
我一直在使用 HList-GHCSyntax 中定义的运算符.
到目前为止它工作得很好,让我可以写这样的东西:
myRecord =
(param1 .=. "param1value") .*.
(param2 .=. "param2value") .*.
emptyRecord
myRecord .!. param1
myRecord .!. param3
param3
,这非常有用是必需的,因为我得到编译时参数检查。
param3
的情况是可选的。我怎样才能做到这一点?
Empty
是一个空类型):
getOptional r l = (hLeftUnion r ((l .=. Empty) .*. emptyRecord)) .!. l
Empty
在调用代码中。
最佳答案
定义 getOptional
的问题正在确定结果类型。如果有人尝试:
class GetOptional r l v | r l -> v where
getOptional :: l -> Record r -> Maybe vs
class GetOptional r l v | r l -> v where
getOptional :: l -> Record r -> Maybe v
v
可以通过查找
l
来确定在
r
如果存在,但如果
l
不在
r
中那么应该从哪里
v
来?选择()还是空?离开函数依赖使用户在某处提供类型注释。
class GetOptional r l v where
getOptional :: l -> v -> Record r -> v
(v->w)
和默认值
w
.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.HList.FakePrelude(HEq,HTrue,HFalse)
import Data.HList.HListPrelude(HNil(HNil),HCons(HCons))
import Data.HList.GhcSyntax((.=.),(.*.))
import Data.HList.Record(Record(Record),LVPair(LVPair),emptyRecord)
class GetOptional l r v where
getOptional :: l -> v -> Record r -> v
instance GetOptional l HNil v where
getOptional _ v _ = v
instance ( HEq l l' b
, GetOptional' b l (HCons (LVPair l' v') r) v
)
=> GetOptional l (HCons (LVPair l' v') r) v where
getOptional l v (Record r) = getOptional' (undefined :: b) l v r
class GetOptional' b l r v where
getOptional' :: b -> l -> v -> r -> v
instance GetOptional' HTrue l (HCons (LVPair l v) r) v where
getOptional' _ _ _ (HCons (LVPair v) _) = v
instance ( GetOptional l r v
)
=> GetOptional' HFalse l (HCons (LVPair l' v') r) v where
getOptional' _ l v (HCons _ r) = getOptional l v (Record r)
data L1 = L1
data L2 = L2
e = emptyRecord
f = L1 .=. True .*. emptyRecord
-- test1 :: Bool
test1 = getOptional L1 False f
-- test2 :: Bool
test2 = getOptional L1 False e
-- test3 :: ()
test3 = getOptional L2 () f
-- test4 gives a type error:
-- test4 = getOptional L1 () f
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.HList.FakePrelude(HFalse,HTrue)
import Data.HList.HListPrelude(HMember,hMember)
import Data.HList.GhcSyntax((.=.),(.*.))
import Data.HList.Record(RecordLabels,Record,HasField(hLookupByLabel),recordLabels,emptyRecord)
-- This type is inferred properly
-- getOptional :: ( RecordLabels r ls
-- , HMember l ls b
-- , GetOptional' b l r v )
-- => l -> v -> Record r -> v
getOptional l v rec = getOptional' (hMember l (recordLabels rec)) l v rec
class GetOptional' b l r v where
getOptional' :: b -> l -> v -> Record r -> v
instance GetOptional' HFalse l rec v where
getOptional' _ _ v _ = v
instance ( HasField l r v )
=> GetOptional' HTrue l r v where
getOptional' _ l _ r = hLookupByLabel l r
data L1 = L1
data L2 = L2
e = emptyRecord
f = L1 .=. True .*. emptyRecord
-- test1 :: Bool
test1 = getOptional L1 False f
-- test2 :: Bool
test2 = getOptional L1 False e
-- test3 :: ()
test3 = getOptional L2 () f
-- test4 gives a type error:
-- test4 = getOptional L1 () f
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.HList.FakePrelude(HFalse,HTrue)
import Data.HList.HListPrelude(HMember,hMember)
import Data.HList.GhcSyntax((.=.),(.*.))
import Data.HList.Record(RecordLabels,Record,HasField(hLookupByLabel),recordLabels,emptyRecord)
import Data.HList.TypeCastGeneric1
import Data.HList.TypeEqGeneric1
import Data.HList.Label5
-- getOptional :: ( RecordLabels r ls
-- , HMember l ls b
-- , GetOptional' b l r v )
-- => l -> Record r -> Maybe v
getOptional l rec = getOptional' (hMember l (recordLabels rec)) l rec
class GetOptional' b l r v where
getOptional' :: b -> l -> Record r -> Maybe v
instance GetOptional' HFalse l rec v where
getOptional' _ _ _ = Nothing
instance ( HasField l r v )
=> GetOptional' HTrue l r v where
getOptional' _ l r = Just (hLookupByLabel l r)
data L1 = L1
data L2 = L2
e = emptyRecord
f = L1 .=. True .*. emptyRecord
test1 = getOptional L1 f
test2 = getOptional L1 e
test3 = getOptional L2 f
-- test4 :: Maybe () -- this would be a type error
-- test4 = getOptional L1 f
main = print ( test1 -- inferred becuase it is Just {}
, test2 :: Maybe () -- must specify for Nothing
, test3 :: Maybe () -- must specify for Nothing
)
关于Haskell:HList 和可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12065246/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!