1

我的任务是在 Ceylon 编写文件编写器,在此过程中,我遇到了在 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");
}

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

}
4

1 回答 1

6

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

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

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

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

||运算符仅适用于Boolean 表达式(即foo is Bar是),而是is Bar foo一个Boolean 条件,这是一个不同的构造。这同样适用于exists条件exists操作符

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

于 2017-06-28T19:41:14.270 回答