2017-06-28 6 views
1

セイロンにファイルライターを書くことを私は任務を務めましたが、その過程で、セイロンにifステートメントを書くことが難しくなりました。セイロンの遠くの土地でのコンパイルの狭い橋の上でタイプ正しさの強大なウィザードが直面している:ifステートメントをセイロンに書く

私が手にエラーが「:(10、1)セイロン:正しくない構文: 『もし』での行方不明EOFエラー」であります

これは私の文の(最初の行は10行である)場合:

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

EDIT: これは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)); 

} 

答えて

7

||オペレータは、if (is ...)と一緒に達成するための正しい方法を使用することはできません何が欲しいのは組合のタイプを使用している:

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

||は、次の構文で有効になりますが、あなたはrefinemenを失うことになりますT(それだけブール式ではなく、型改良であろう):is Bar foo異なる構築物であるBooleancondition、ある一方

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

||オペレータのみ、(foo is Barである)Boolean上で動作。 exists conditionsexists operatorsの場合も同様です。

編集:ああ、もちろんifステートメントを関数toplevel elements can only be declarations(クラス、関数または値のif文は使用できません)に入れる必要があります。

+0

チップのおかげで。私はそれに応じて更新しました。ただし、エラーメッセージは同じままです。 – loldrup

+2

私のコメントを編集しました。関数に 'if'を入れる必要があります。 –

+0

これで、ファイルに何も書き込まれない限り(これは作成されるだけです)、動作します。私は単に、私の最上位レベルの行をrunServer関数に置きます。 – loldrup