質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Haskell

Haskellは高い機能性をもった関数型プログラミング言語で、他の手続き型プログラミング言語では難しいとされている関数でも容易に行うことができます。強い静的型付け、遅延評価などに対応しています。

Q&A

解決済

1回答

1607閲覧

YesodでA Yesod tutorialの3.Mirrorのサンプルが実行できません

apo

総合スコア349

Haskell

Haskellは高い機能性をもった関数型プログラミング言語で、他の手続き型プログラミング言語では難しいとされている関数でも容易に行うことができます。強い静的型付け、遅延評価などに対応しています。

0グッド

0クリップ

投稿2018/09/27 11:28

前提・実現したいこと

YesodでShakespearean Templatesを使用したGET POSTを実行したい。
公式サイト(A Yesod tutorial)のサンプルを入力しビルドしたのですが、下記のようなエラーがでます。

発生している問題・エラーメッセージ

サンプルコードを入力し、ビルドすると下記のようなエラーがでます。

[apo@localhost my-project]$ stack build Warning: /home/apo/doc/dev/making/Haskell/hs_yesod_mirror/my-project/my-project.cabal was modified manually. Ignoring /home/apo/doc/dev/making/Haskell/hs_yesod_mirror/my-project/package.yaml in favor of the cabal file. If you want to use the package.yaml file instead of the cabal file, then please delete the cabal file. my-project-0.0.0: unregistering (components added: exe:my-project) my-project-0.0.0: configure (lib + exe) Configuring my-project-0.0.0... my-project-0.0.0: build (lib + exe) Preprocessing library for my-project-0.0.0.. Building library for my-project-0.0.0.. [ 1 of 13] Compiling Model ( src/Model.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Model.o ) [ 2 of 13] Compiling Settings ( src/Settings.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Settings.o ) [ 3 of 13] Compiling Settings.StaticFiles ( src/Settings/StaticFiles.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Settings/StaticFiles.o ) [ 4 of 13] Compiling Import.NoFoundation ( src/Import/NoFoundation.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Import/NoFoundation.o ) [ 5 of 13] Compiling Paths_my_project ( .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/autogen/Paths_my_project.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Paths_my_project.o ) [ 6 of 13] Compiling Foundation ( src/Foundation.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Foundation.o ) [ 7 of 13] Compiling Import ( src/Import.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Import.o ) [ 8 of 13] Compiling Handler.Profile ( src/Handler/Profile.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Handler/Profile.o ) [ 9 of 13] Compiling Handler.Mirror ( src/Handler/Mirror.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Handler/Mirror.o ) /home/apo/doc/dev/making/Haskell/hs_yesod_mirror/my-project/src/Handler/Mirror.hs:7:30: error: ~ Couldn't match expected type ‘WidgetFor App ()’ with actual type ‘Language.Haskell.TH.Syntax.Q Language.Haskell.TH.Syntax.Exp’ ~ In the second argument of ‘($)’, namely ‘(widgetFile "mirror")’ In the expression: defaultLayout $ (widgetFile "mirror") In an equation for ‘getMirrorR’: getMirrorR = defaultLayout $ (widgetFile "mirror") | 7 | getMirrorR = defaultLayout $(widgetFile "mirror") | ^^^^^^^^^^^^^^^^^^^ /home/apo/doc/dev/making/Haskell/hs_yesod_mirror/my-project/src/Handler/Mirror.hs:11:53: error: ~ Couldn't match expected type ‘Text’ with actual type ‘[Char]’ ~ In the second argument of ‘ireq’, namely ‘"content"’ In the second argument of ‘($)’, namely ‘ireq textField "content"’ In a stmt of a 'do' block: postedText <- runInputPost $ ireq textField "content" | 11 | postedText <- runInputPost $ ireq textField "content" | ^^^^^^^^^ /home/apo/doc/dev/making/Haskell/hs_yesod_mirror/my-project/src/Handler/Mirror.hs:12:25: error: ~ Couldn't match expected type ‘WidgetFor App ()’ with actual type ‘Language.Haskell.TH.Syntax.Q Language.Haskell.TH.Syntax.Exp’ ~ In the second argument of ‘($)’, namely ‘(widgetFile "posted")’ In a stmt of a 'do' block: defaultLayout $ (widgetFile "posted") In the expression: do postedText <- runInputPost $ ireq textField "content" defaultLayout $ (widgetFile "posted") | 12 | defaultLayout $(widgetFile "posted") | ^^^^^^^^^^^^^^^^^^^ -- While building custom Setup.hs for package my-project-0.0.0 using: /home/apo/.stack/setup-exe-cache/x86_64-linux/Cabal-simple_mPHDZzAJ_2.2.0.1_ghc-8.4.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-2.2.0.1 build lib:my-project exe:my-project --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always" Process exited with code: ExitFailure 1

該当のソースコード

src/Handler/Mirror.hs

module Handler.Mirror where import Import import qualified Data.Text as T getMirrorR :: Handler Html getMirrorR = defaultLayout $(widgetFile "mirror") postMirrorR :: Handler Html postMirrorR = do postedText <- runInputPost $ ireq textField "content" defaultLayout $(widgetFile "posted")

templates/mirror.hamlet

<h1> Enter your text <form method=post action=@{MirrorR}> <input type=text name=content> <input type=submit>

templates/posted.hamlet

<h1>You've just posted <p>#{postedText}#{T.reverse postedText} <hr> <p><a href=@{MirrorR}>Get back

試したこと

hamlet内の引数を削除してみましたが、エラーが改善されませんでした。
引数の問題ではないようです。

補足情報(FW/ツールのバージョンなど)

stack version : 1.7.1
lts : lts-12.10

ファイル作成までの流れ

stack new my-project yesod-sqlite && cd my-project stack install yesod-bin --install-ghc yesod add-handler Name of route (without trailing R): Mirror Enter route pattern (ex: /entry/#EntryId): /mirror Enter space-separated list of methods (ex: GET POST): GET POST

src/Foundation.hsファイルに下記を追加

isAuthorized MirrorR _ = return Authorized

全ソースコード
https://github.com/ringo-apo/hs_yesod_mirror

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

src/Handler/Mirror.hs で次の言語拡張を有効にするとコンパイルできます。(ファイルの先頭に追記してください。)

haskell

1{-# LANGUAGE OverloadedStrings #-} 2{-# LANGUAGE TemplateHaskell #-}

Overloaded Strings は "foo" の型を String ではなく IsString a => a にするものです。

Template Haskell はコンパイル時コード生成をするためのもので $(foo) という構文を有効にします。

投稿2018/09/27 15:54

編集2018/09/27 15:56
kakkun61

総合スコア285

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

apo

2018/09/27 22:43

コンパイルできました! すごい!! こうやって型をあわせるのですね。 ありがとうございます。
kakkun61

2018/09/28 01:56

Yesod は言語拡張がふんだんに使われていて標準な Haskell の入門書を読むだけでは分からないことが多いと思います。 手前味噌ですが、Yesod で使われる言語拡張も一通り解説した『Haskell で作る Web アプリケーション』という書籍がありますのでよければ参考にしてみてください。 https://nextpublishing.jp/book/9979.html
apo

2018/09/28 03:01

その本は持っているのですが、ここの部分はよく理解しておりませんでした。読み返してみます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問