我认为对于 julius 中的拼接是如何工作的,我有一些不明白的地方,内置类型都不会拼接。我可以编译代码的唯一方法是使用 rawJS。
例如:
import Prelude.Unicode
import Text.Julius
import Text.Shakespeare -- not sure if this is needed
import Text.Shakespeare.Text -- not sure if this is needed
...
test = renderJavascript $ jsCode (⊥)
where
yval = rawJS $ show (3 ∷ Int) -- works
-- yval = show (3 ∷ Int) -- no instance of toJavascript
-- yval = 3 ∷ Int -- no instance of toJavascript
jsCode = [js|
var y = #{yval};
|]
FWIW我没有使用yesod,只是图书馆的朱利叶斯模板部分,但我认为这不重要。
如果我尝试拼接 Int 本身,我会收到如下错误:
No instance for (ToJavascript Int)
arising from a use of ‘toJavascript’
In the expression: toJavascript yval
In the first argument of ‘mconcat’, namely
‘[Javascript
((Data.Text.Internal.Builder.fromText . pack')
"\n\
\ var y = "),
toJavascript yval,
Javascript
((Data.Text.Internal.Builder.fromText . pack')
";\n\
\ ")]’
In the expression:
mconcat
[Javascript
((Data.Text.Internal.Builder.fromText . pack')
"\n\
\ var y = "),
toJavascript yval,
Javascript
((Data.Text.Internal.Builder.fromText . pack')
";\n\
\ ")]