2010-05-28 2 views
1

정수 표현식을 문자 리터럴과 비교하려고하는데 컴파일러가 형식 불일치에 대해 불평합니다.F #가 char 값과 일치합니다.

let rec read file includepath = 
    let ch = ref 0 
    let token = ref 0 
    use stream = File.OpenText file 

    let readch() = 
     ch := stream.Read() 
    let lex() = 
     match !ch with 
     | '!' -> 
      readch() 
     | _ -> token := !ch 

채널은 사용하기 -1 파일 마커의 끝으로 순서대로 반환 stream.Read 무엇 때문에 INT 수 있습니다. '!'int '!'으로 바꾸어도 여전히 작동하지 않습니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

답변

4
open System.IO 
let rec read file includepath = 
    let ch = ref '0' 
    let token = ref '0' 
    use stream = File.OpenText file 

    let readch() = 
     let val = stream.Read(); 
     if val = -1 then xxx 
     else 
      ch := (char)(val) 
      xxx 
    let lex() = 
     match !ch with 
     | '!' -> 
      readch() 
     | _ -> token := !ch 


    0 

더 나은 스타일 : 그들이 작곡을 깰로

let rec read file includepath = 
    use stream = File.OpenText file 

    let getch() = 
     let ch = stream.Read() 
     if ch = -1 then None 
     else Some(char ch) 

    let rec getToken() = 
     match getch() with 
      | Some ch -> 
       if ch = '!' then getToken() 
       else ch 
      | None -> 
       failwith "no more chars" //(use your own excepiton) 
+0

,하지만 어떻게 처리합니까 -1 파일 마커의 끝? – rwallace

+0

@ 그냥 값을 먼저 얻은 다음 유형 변환 –

4

은 F # 언어가 (유형 간의 암시 적 대화를하지 않는 즉, 당신은 더 이상 암시가 없을 것 같은이 평균의 변경 작업을 이동하는 경우 변환). 당신은 문자로 스트림에 의해 반환 된 INT 변경하려면 char 연산자를 사용할 수 있습니다 확실히, 문자 값을 집어

open System.IO 
let rec read file includepath = 
    let ch = ref 0 
    let token = ref 0 
    use stream = File.OpenText file 

    let readch() = 
     ch := stream.Read() 
    let lex() = 
     match char !ch with 
     | '!' -> 
      readch() 
     | _ -> token := !ch 
    lex() 
관련 문제