【问题标题】:Code compiles with netcore but will not compile using mono代码使用 netcore 编译,但不会使用 mono 编译
【发布时间】:2019-01-29 20:47:58
【问题描述】:

我正在运行Ubuntu 18.04 LTS,使用单声道版本5.18.0.2140,如果我将netcore 与VS Code 一起使用,我的代码将不会在单声道下编译,但它会编译。但是,我需要使用单声道来完成这项作业,所以我不确定出了什么问题。我正在使用的命令是:

fsharpc --nologo chessApp.fsx && mono chessApp.exe

错误发生在这部分代码(from this line and down,底部的完整要点):

let playerOne = Chess.Human(Color.White) // Here
let playerTwo = Chess.Human(Color.Black) // Here

let game = new Chess.Game()
game.run(playerOne, playerTwo, board, pieces) // and here

我得到的错误如下:

 chessApp.fsx(28,17): The object constructor 'Human' takes 0 argument(s) but is here given 1. The requried signature is 'new : unit -> Human'.

 chessApp.fsx(29,17): The object constructor 'Human' takes 0 argument(s) but is here given 1. The requried signature is 'new : unit -> Human'.

 chessApp.fsx(31,1): This value is not a function and cannot be applied.

我没有使用 VS Code 得到这些错误。它在那里工作。例如,Human 在 VS Code 上确实需要 1 个参数 netcore 可以识别,但 mono 不会。为了不让这个问题变得更长,我已将代码上传到 gist right here

【问题讨论】:

    标签: compiler-errors f# .net-core mono


    【解决方案1】:

    我尝试通过在 Windows 上从命令行运行编译器来重现这一点。您的fsx 源文件中有两个小错误。首先,你打开了一个错误的命名空间:

    #r "chess.dll"
    #r "pieces.dll"
    
    open Chess
    open Pieces // <- This should be open 'Piece'
    

    其次,pieces 集合需要是一个列表,而不是一个数组:

    let pieces =
       [ king (White) :> chessPiece
         rook (White) :> chessPiece
         king (Black) :> chessPiece
         rook (Black) :> chessPiece ]
    

    通过这两个更改,我能够编译所有内容:

    fsharpc --target:library chess.fs
    fsharpc --target:library -r:chess.dll pieces.fs
    fsharpc chessApp.fsx
    

    请注意,您需要--target:library 来指示这应该构建一个dll 文件,还需要-r:chess.dll 来告诉它需要引用第一个dll 的第二个文件。

    如果您使用#load 将另外两个文件作为源文件引用而不是使用#r 作为编译文件,这将容易得多:

    #load "chess.fs"
    #load "pieces.fs"
    

    然后你可以通过运行fsharpc chessApp.fsx来编译整个东西,你会得到一个独立的可执行文件。

    【讨论】:

    • 天哪,我完全看过了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    相关资源
    最近更新 更多