2016-11-12 1 views
2

Ocaml에서 React의 유형화 된 버전을 만들려고합니다. 더 기능적으로 만들기 위해 레코드를 렌더링 인수로 전달합니다. 컴파일러는 let newInstance = { props; state = nextState; updater = (mkUpdater props) }updater = (mkUpdater props)의 유형을 추론 할 수없는 이유 이해가 안Ocaml 유형 추정이 잘못되었습니다.

type ('props,'state) reactInstance = 
    { 
    props: 'props; 
    state: 'state; 
    updater: 'a . ('props,'state) reactInstance -> 'a -> 'state -> unit;} 
and ('props,'state) reactClass = 
    { 
    getInitialState: unit -> 'state; 
    render: ('props,'state) reactInstance -> element;} 

module type ComponentBluePrint = 
    sig 
    type props 
    type state 
    val getInitialState : unit -> state 
    val render : (props,state) reactInstance -> element 
    end 

module type ReactClass = 
    sig 
    type props 
    type state 
    val mkUpdater : 
    props -> 
    ((props,state) reactInstance -> 'e -> state) -> 
     (props,state) reactInstance -> 'e -> unit 
    val reactClass : (props,state) reactClass 
    end 

module CreateComponent(M:ComponentBluePrint) = 
    (struct 
    include M 
    let rec mkUpdater props f i e = 
     let nextState = f i e in 
     let newInstance = 
     { props; state = nextState; updater = (mkUpdater props) } in 
     () 

    let reactClass = 
     { render = M.render; getInitialState = M.getInitialState } 
    end : (ReactClass with type props = M.props and type state = M.state)) 

한 가지입니다.

Error: Signature mismatch: 
     Values do not match: 
     let mkUpdater : 
    props => 
    (reactInstance props state => '_a => '_b) => 
    reactInstance props state => '_a => unit 
     is not included in 
     let mkUpdater : 
    props => 
    (reactInstance props state => 'e => state) => 
    reactInstance props state => 'e => unit 

'_a와'e의 차이점은 무엇입니까? 정확히 똑같은 것처럼 보입니다. 이 유형을 확인하려면 어떻게해야합니까?

+0

제공하신 오류는 귀하의 파일이 생성 한 실제 오류가 아닙니다. 올바른 오류는 "오류 :이 표현식은 ('a ->'b -> c) -> 'a ->'b -> 'd 이지만 표현식은 ('e, 'c) reactInstance -> 'f ->'c -> 단위 유형 'a ->'b -> 'c는 유형 ('e, 'c)과 호환되지 않습니다. reactInstance " – Drup

+0

다음과 같은 여러 가지 오류가 있습니다. 파일이지만, 작업의 의미가 무엇인지 모른 채이를 해결하는 것은 어렵습니다. 첫 번째로 나에게로 건너 뛰는 것은 업데이터 필드에서''a''입니다. 여기에서 forall을 사용 하시겠습니까? 매우 유용하지는 않습니다. – Drup

답변

2

유형 변수 '_a (실제 문자는 중요하지 않습니다. 중요한 것은 밑줄 임)은 약한 유형 변수입니다. 이것은 일반화 될 수없는 변수입니다. 즉 하나의 구체적인 유형으로 대체 될 수 있습니다. 이것은 가변적 인 가치와 비슷하지만, 유형의 영역에 있습니다.

약한 유형 변수 '_a으로 표시된 유형은 제네릭 형식 변수로 표시된 유형에 포함되지 않습니다. 또한, 컴파일 유닛을 벗어날 수 없으며, 숨겨 지거나 구체화되어야합니다.

약식 변수는 표현식이 순수 값 (구문 적으로 정의 됨)이 아닌 경우 만들어집니다. 일반적으로 함수 응용 프로그램이거나 추상화입니다. 모든 함수 인수를 열거하여 보통 함수 응용 프로그램에 부분 적용 함수를 대체 할 때 (예 : updater = (fun props f i e -> mkUpdater props f i e)), 소위 η 확장을 수행하여 약한 유형 변수를 제거하는 것이 보통 가능합니다.

+0

큰 설명에 감사드립니다! – Seneca

관련 문제