2009-09-15 5 views

답변

8

당신은 할 수 있습니다.

7

하나의 옵션은 polymorphic variants입니다.

# type mylist = [`I of int | `S of string] list ;; 
type mylist = [ `I of int | `S of string ] list 

을 다음과 같은 값을 정의 : 당신은 사용하여 목록의 유형을 정의 할 수 있습니다

다형성 변종 "열기"유형이기 때문에 당신은,하지만, 형의 주석을 추가 할 조심해야
# let r : mylist = [`I 10; `S "hello"; `I 0; `S "world"] ;; 
val r : mylist = [`I 10; `S "hello"; `I 0; `S "world"] 

. 예를 들어, 다음은 법적 :

# let s : mylist = [`I 0; `S "foo"; `B true];; 
This expression has type [> `B of bool ] but is here used with type 
    [ `I of int | `S of string ] 
The second variant type does not allow tag(s) `B 
:

# let s = [`I 0; `S "foo"; `B true] 
val s : [> `B of bool | `I of int | `S of string ] list = 
    [`I 0; `S "foo"; `B true] 

사용, 비 정수 또는 문자열 값을 허용에서 주석을 목록의 유형을 방지하기 위해

관련 문제