【问题标题】:How do I create a foreign constant in PureScript?如何在 PureScript 中创建外部常量?
【发布时间】:2015-08-05 18:26:05
【问题描述】:

我正在尝试在 PureScript 中创建一个外部常量,但它似乎没有调用该函数。

我在 PureScript 中:

module Test where

foreign import test :: String

foreign import test2 :: String -> String

在 JavaScript 中:

"use strict";

// module Test

exports.test = function() {
    return "A";
};

exports.test2 = function(x) {
    return x;
};

但它不调用外部函数:

> import Prelude
> :t test
Prim.String
> :t test2
Prim.String -> Prim.String
> test
undefined

> test2 "test"
"test"
> test ++ "A"
"function () {\n    return \"A\";\n}A"

是否可以创建一个外部常量?还是所有函数都至少有一个参数?我正在使用:

$ pulp psci --version
0.7.0.0

【问题讨论】:

    标签: javascript string constants purescript


    【解决方案1】:

    您不需要额外的功能。 String 的运行时表示只是一个字符串!

    "use strict";
    
    // module Test
    
    exports.test = "A";
    

    test2 是正确的,但是。 -> 的运行时表示是一个单参数 Javascript 函数,正如您已经拥有的那样。

    【讨论】:

    • 谢谢!我期待一个没有参数的函数是一个常数,但这是有道理的。
    猜你喜欢
    • 2023-03-20
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多