2009-05-11 4 views
6

나는 내 책을 다 읽었으며 검색어가 다 떨어질 때까지 봤지만 아직이 문제에 대한 예나 대답을 찾을 수 없습니다.F # 유형 참조 오류를 어떻게 해결합니까?

다음 코드는 컴파일되지 않습니다. Entity가 선언 될 때 Type Effect와 Type Affect가 선언되지 않았기 때문입니다. 그래서 내가 이해할 수없는 것은 이것을 어떻게 해결할 것인가입니다.

C++에서이 문제는 h 파일의 프로토 타입 선언을 통해 해결 된 다음 h 파일을 포함하여 해결됩니다. C#에서는 결코 문제가되지 않습니다. 그렇다면 F #에서 어떻게 해결됩니까? 여기

#light 
type Entity = 
    { 
     Name:string; 
     Affects:List<Affect>; //Compile error: The type Affect is not defined 
     Effects:List<Effect>; //Compile error: the type Effect is not defined 
    } 

type Effect = 
    { 
     Name:string; 
     //A function pointer for a method that takes an Entity and returns an Entity 
     ApplyEffect:Entity -> Entity; 
    } 

type Affect = 
    { 
     Name:string; 
     //A List of Effects that are applied by this Affect Object 
     EffectList:List<Effect>; 
     //A function pointer to return an Entity modified by the listed Effects 
     ApplyAffect:Entity->Entity; 
    } 

근본적인 목표는 유형의 엔티티의 객체가이 유형 엔터티의 개체에 적용 할 수 있습니다에 영향을 나열 할 수 있어야한다는 것입니다. 엔터티는 적용된 효과를 나열 할 수도 있습니다. 이 방법으로 엔티티의 "현재"상태는 모든 엔티티를 원래 엔티티 상태에 대해 폴딩하여 찾습니다.

내가이 정답이라고 생각, 당신의 시간을

--Adam Lenda

+0

참조 또한 http://stackoverflow.com/questions/680606/f-how-to-have-two-methods-calling-each-other wh ich는 상호 재귀 적 유형뿐만 아니라 상호 재귀 적 함수를 정의하는 방법을 보여줍니다. – Brian

답변

13

감사합니다 ... 그래서

http://langexplr.blogspot.com/2008/02/defining-mutually-recursive-classes-in.html

type Entity = 
    { 
     Name:string; 
     Affects:List<Affect>; 
     Effects:List<Effect>; 
    } 
and Effect = 
    { 
     Name:string; 
     ApplyEffect:Entity -> Entity; 
    } 
and Affect = 
    { 
     Name:string; 
     EffectList:List<Effect>; 
     ApplyAffect:Entity->Entity; 
    } 
+0

예 - 방금 게시하려고했던 것입니다. 이제 발견했습니다. "Functional Programming for the Real World"에서 8 장. –

+0

Chapter 3, 67 페이지의 'expert f #' –

+0

Hrmm ... 나는 똑같은 것을 게시하려했으나 컴파일러 오류를 얻은 후에는 불가능하다고 결론 지었다. ! 내가 구문을 약간 잘못 알아 냈어. – Noldorin

관련 문제