2012-07-05 5 views
3

F # 대화식에서 sprintf 유형을 찾을 수 있습니다. 카레 기능은 일반적인없는 경우카툰 함수에 유형 주석을 추가 할 위치는 어디입니까?

>sprintf;; 
val it : (Printf.StringFormat<'a> -> 'a) = <fun:[email protected]> 

나는 첫 번째 매개 변수와 sprintf와의 카레의 종류를 찾을 수 있습니다.

> sprintf "%g";; 
val it : (float -> string) = <fun:[email protected]> 

그러나 일반적인 경우 값 제한 오류가 발생합니다.

> sprintf "%A";; 
error FS0030: Value restriction. The value 'it' has been inferred to have generic type 
val it : ('_a -> string)  
Either make the arguments to 'it' explicit or, if you do not intend for it to be generic, add a type annotation. 

나는 예를 들면 유형에 대한 기능, 전문, 같은 값 제한을 제거 할 수있는 유형의 주석을 추가 할 수 있습니다. 날짜 시간.

>let f : (DateTime -> string) = sprintf "%A";; 
val f : (DateTime -> string) 

바인딩없이 유형 주석을 추가하려면 어떻게해야합니까? 내가 ...

>sprintf "%A" : (DateTime -> string);; 
error FS0010: Unexpected symbol ':' in interaction. Expected incomplete structured construct at or before this point, ';', ';;' or other token. 

이 비슷한 예입니다 만 열심히 ... 다음 시도했습니다

>sprintf "%a";; 
error FS0030: Value restriction. The value 'it' has been inferred to have generic type 
val it : ((unit -> '_a -> string) -> '_a -> string)  
Either make the arguments to 'it' explicit or, if you do not intend for it to be generic, add a type annotation. 

답변

3

수 있습니다.

+2

또한 주석을 단순화하고'DateTime' 부분 만 지정할 수도 있습니다 :'(sprintf "% A": DateTime -> _)'. 컴파일러는 여전히 반환 유형을 추론 할 수 있습니다. –

+0

부분 주석이 효과적입니다. 더 어려운 예제는 다음과 같습니다. (sprintf "% a": (_ -> DateTime -> string) -> _ -> _). – philderbeast

1

무엇 실제로 일어나는 것은 FSI 당신이라는 변수에 입력 한 마지막을 결합이다 it. 그것은 효과적으로

let it = sprintf "%a";; 

유형 주석이 =의 왼쪽에있는 액세스 할 수 없습니다 이동해야한다. 문제는 임의의 변수 (이 경우 it)에 제공 할 구체적인 유형이 필요하다는 것입니다. 당신이 바인딩없이 유형의 주석을 지정할 수 있습니다

open System;; 
(sprintf "%A" : DateTime -> string);; 

val it : (DateTime -> string) = <fun:[email protected]> 

그 방법 : 해결 방법은 그냥 괄호 안에 표현식을 묶어야합니다

(fun t:DateTime -> sprintf "%a" t) 
관련 문제