ブーメランを適切に使用して URL を生成する方法について、私は少し混乱しています。私は次のものを持っています:
data State =
AK | AL | AR | AZ | CA ... WY
data Sitemap
= Home
| State State
| Place State String
deriving (Eq, Ord, Read, Show, Data, Typeable)
$(derivePrinterParsers ''Sitemap)
sitemap ∷ Router Sitemap
sitemap =
( rHome
<> rState . state
<> rPlace . (state </> anyString)
)
state :: PrinterParser StringsError [String] o (State :- o)
state = xmaph read (Just . show) anyString
state
これは機能しているように見えますが、 の実装と のドキュメントの実装を比較するとarticleId
、反対の方法で動作しているように見えます。
articleId :: Router ArticleId
articleId = xmaph ArticleId (Just . unArticleId) int
タイプはまったく異なり、逆方向に進んでいるように見えますが、私のsitemap
作品とアプリは正しく URL を処理します。私はそれがもっとこのように見えるべきだと思います:
maybeState :: String → Maybe State
maybeState stateString = case reads stateString of
[(state, "")] -> Just state
_ -> Nothing
stateR :: Router State
stateR = xpure show maybeState
これは型チェックを行いませんが、上記のundefined
定義を置き換えても機能しますが、機能しません。sitemap
rState . stateR
rPlace . (stateR </> anyString)
おそらくこれを処理するライブラリ関数があるので、これは十分に頻繁に発生するようですが、私はそれを見ませんでした。
編集:ここに私が得る型エラーのいくつかがあります:
の場合state = xpure show maybeState
:
Main.hs:56:16:
Couldn't match expected type `State :- ()'
with actual type `[Char]'
Expected type: () -> State :- ()
Actual type: () -> String
In the first argument of `xpure', namely `show'
In the expression: xpure show maybeState
For state = undefined :: Router State
(このエラーはsitemap
定義にあります):
Main.hs:45:18:
Couldn't match expected type `String :- ()' with actual type `()'
Expected type: PrinterParser
StringsError [String] () (State :- (String :- ()))
Actual type: Router State
In the first argument of `(</>)', namely `state'
In the second argument of `(.)', namely `(state </> anyString)'