在 Ceylon 中编写 if 语句

Writing an if statement in Ceylon

我给自己分配了在锡兰写一个文件编写器的任务,在这样做的过程中,我被在锡兰写一个 if 语句的巨大困难所打击,面对强大的向导时,它会被允许通过在遥远的锡兰的狭窄编译桥上的类型正确性:

我得到的错误是"Error:(10, 1) ceylon: incorrect syntax: missing EOF at 'if'"

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

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

编辑: 这是我根据 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");
}

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

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));

}

|| 运算符不能与 if (is ...) 一起使用,实现你想要的正确方法是使用联合类型:

if (is Nil|File fileResource) {
    ...
}

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

if (fileResource is Nil || fileResource is File ) {
    ...
}

|| 运算符仅适用于 Boolean 表达式 foo is Bar 是),而 is Bar fooBoolean condition, which is a different construct. The same applies for exists conditions vs exists operators.

编辑:哦,当然你需要把那个if语句放在一个函数中,toplevel elements can only be declarations(类,函数或值,如果语句是不允许的)。