【问题标题】:Mozart error "illegal use of nesting marker" when creating a functor创建函子时莫扎特错误“非法使用嵌套标记”
【发布时间】:2022-01-17 19:08:35
【问题描述】:

在使用函子编译 Oz 代码时,我在声明“函子”的行上收到错误“非法使用嵌套标记”。这意味着什么?

functor
export
    sum:Sum
    divisao:Div
    mult:Mult
    sub:Sub
define
    fun {Sum X Y} X + Y end
    fun {Mult X Y} X * Y end
    fun {Sub X Y} X - Y end
end

【问题讨论】:

    标签: oz mozart


    【解决方案1】:

    首先,您缺少 Div 的定义。

    functor
    export
       sum:Sum
       divisao:Div
       mult:Mult
       sub:Sub
    define
       fun {Sum X Y} X + Y end
       fun {Mult X Y} X * Y end
       fun {Sub X Y} X - Y end
       fun {Div X Y} X / Y end
    end
    

    functors 是定义导入和导出的模块,作为文件。您应该编译该文件并将其转换为模块。例如,调用你的文件 test.oz 并运行以下命令编译运行。

    ozc -c test.oz && ozengine test.ozf
    

    如果您通过向编译器提供缓冲区来运行 Mozart,则不能直接使用 functor,因为您必须将其转换为模块。您必须先声明它,然后使用 Module.manager 应用它。

    declare F M ModMan
    F = functor
        export
            sum:Sum
            divisao:Div
            mult:Mult
            sub:Sub
        define
            fun {Sum X Y} X + Y end
            fun {Mult X Y} X * Y end
            fun {Sub X Y} X - Y end
            fun {Div X Y} X / Y end
        end
    
    % To use the functions of the functor, apply it and create a module
    ModMan = {New Module.manager init}
    M = {ModMan apply(F $)}
    
    % Then use the exported functions with module M
    % Example:
    {Show {M.sum 3 5}}
    % >>> 8
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多