[英]How can I use (^? ix 0) in an independent Getter?
抱歉這個措辭不好的標題,但我甚至不知道如何正確地問它。
我怎么能這個呢?
instPublicIP :: Instance -> Maybe Text
instPublicIP inst =
inst ^. insNetworkInterfaces ^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just
進入這個
instPublicIP' :: Lens' Instance (Maybe Text)
instPublicIP' = insNetworkInterfaces ^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just
當我嘗試時,我收到以下錯誤:
Main.hs:198:3:
Couldn't match expected type ‘(Maybe Text -> f (Maybe Text))
-> Instance -> f Instance’
with actual type ‘Maybe Text’
Relevant bindings include
instPublicIP' :: (Maybe Text -> f (Maybe Text))
-> Instance -> f Instance
(bound at app/Main.hs:197:1)
In the expression:
insNetworkInterfaces
^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just
In an equation for ‘instPublicIP'’:
instPublicIP'
= insNetworkInterfaces
^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just
Main.hs:198:27:
Couldn't match type ‘InstanceNetworkInterface’
with ‘Instance -> f0 Instance’
Expected type: (InstanceNetworkInterface
-> Const (Data.Monoid.First Text) InstanceNetworkInterface)
-> (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)
-> Const
(Data.Monoid.First Text)
(([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)
Actual type: (IxValue
(([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)
-> Const
(Data.Monoid.First Text)
(IxValue
(([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)))
-> (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)
-> Const
(Data.Monoid.First Text)
(([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
-> Instance -> f0 Instance)
In the first argument of ‘(.)’, namely ‘ix 0’
In the second argument of ‘(^?)’, namely
‘ix 0 . iniAssociation . _Just . iniaPublicIP . _Just’
結果我只需要更換^?
與.
並將Lens'
改為Traversal'
instPublicIP' :: Traversal' Instance (Maybe Text)
instPublicIP' = insNetworkInterfaces . ix 0 . iniAssociation . _Just . iniaPublicIP
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.