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.
또한 주석을 단순화하고'DateTime' 부분 만 지정할 수도 있습니다 :'(sprintf "% A": DateTime -> _)'. 컴파일러는 여전히 반환 유형을 추론 할 수 있습니다. –
부분 주석이 효과적입니다. 더 어려운 예제는 다음과 같습니다. (sprintf "% a": (_ -> DateTime -> string) -> _ -> _). – philderbeast