在锡兰写一个if语句

lol*_*rup 1 ceylon

我的任务是在锡兰写一个文件编写者,在这样做的过程中,我遇到了在锡兰写一个if语句的困难,当面对强大的类型正确的向导时,它将被允许通过.在锡兰远处的狭窄的编译桥:

我得到的错误是"错误:(10,1)锡兰:语法不正确:在'if'处缺少EOF"

这是我的if语句(第一行是第10行):

if (is Nil fileResource || is File fileResource) {
    File file = createFileIfNil(fileResource);
    value writer = file.Overwriter();
    //writer.writeLine("Hello, World!");
} else {
    print("hello");
}
Run Code Online (Sandbox Code Playgroud)

编辑:根据Bastien Jansens的建议,这是我的if语句更新.但是,错误仍然是相同的:(

Path folderPath = parsePath("""C:\Users\Jon\Auchitect\POSTtoFile""");
Path filePath = folderPath.childPath("BPset.json.txt");
FResource fileResource = filePath.resource;
if (is Nil|File fileResource) {
    File file = createFileIfNil(fileResource);
    value writer = file.Overwriter();
    //writer.writeLine("Hello, World!");
} else {
    print("hello");
}
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序的完整源代码:

import ceylon.http.server { newServer, startsWith, Endpoint, Request, Response }
import ceylon.io { SocketAddress }
import ceylon.file { Path, parsePath, File, createFileIfNil, FResource = Resource }


// let's create a file with "hello world":
Path folderPath = parsePath("""C:\Users\Jon\Auchitect\POSTtoFile""");
Path filePath = folderPath.childPath("BPset.json.txt");
FResource fileResource = filePath.resource;
if (is Nil|File fileResource) {
    File file = createFileIfNil(fileResource);
    value writer = file.Overwriter();
    //writer.writeLine("Hello, World!");
} else {
    print("hello");
}



shared void runServer() {

    //create a HTTP server
    value server = newServer {
        //an endpoint, on the path /hello
            Endpoint {
                path = startsWith("/postBPset");
                //handle requests to this path
                function service(Request request, Response response) {
                    variable String logString;
                    variable String jsonString;
                    variable String contentType;
                    contentType = request.contentType
                        else "(not specified)";
                    logString = "Received " + request.method.string + " request \n"
                    + "Content type: " + contentType + "\n"
                    + "Request method: " + request.method.string + "\n";
                    jsonString = request.read();
                    print(logString);
                    return response;
                }

            }
    };

    //start the server on port 8080
    server.start(SocketAddress("127.0.0.1",8080));

}
Run Code Online (Sandbox Code Playgroud)

Bas*_*sen 7

||运营商不能配合使用if (is ...),正确的方式来实现你想要的是使用的联合类型:

if (is Nil|File fileResource) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

|| 将在以下语法中有效,但您将失去细化(它只会是布尔表达式,而不是类型细化):

if (fileResource is Nil || fileResource is File ) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

所述||操作者仅适用于Boolean 表达式(这foo is Bar是),而is Bar foo是一个Boolean 条件,这是一个不同的构建体.这同样适用于exists条件 VS exists运营商.

编辑:哦,当然你需要将该if语句放在一个函数中,顶级元素只能是声明(类,函数或值,if语句是不允许的).

  • 我编辑了我的评论,你需要将`if`放在一个函数中. (2认同)
  • 啊乔恩,记得要冲洗;) (2认同)