我的任务是在锡兰写一个文件编写者,在这样做的过程中,我遇到了在锡兰写一个if语句的困难,当面对强大的类型向导时,它将被允许通过在锡兰远山地区狭窄的编辑桥上的正确性:
我得到的错误是"错误:(10,1)锡兰:语法不正确:如果'"
,则错过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));
}
答案 0 :(得分:7)
||
运算符不能与if (is ...)
一起使用,实现所需内容的正确方法是使用联合类型:
if (is Nil|File fileResource) {
...
}
||
在以下语法中有效,但您将失去细化(它只会是布尔表达式,而不是类型细化):
if (fileResource is Nil || fileResource is File ) {
...
}
||
运算符仅适用于Boolean
表达式(foo is Bar
是),而is Bar foo
是Boolean
condition,这是一个不同的结构。这同样适用于exists
conditions与exists
operators。
编辑:哦,当然你需要将if
语句放在函数toplevel elements can only be declarations中(类,函数或值,如果不允许语句)。