OCaml의 객체 지향 구조 및 유형 시스템을 사용하여 방문자 디자인 패턴을 구현하려고 시도 중이고 요소의 인스턴스 생성시 문제가 발생했습니다.OCaml의 방문객 디자인 패턴
class virtual ['hrRep] employee = object
method virtual receiveEvaluation : 'hrRep -> unit
method virtual getName : string
end;;
class ['hrRep] accountant myName = object (self : 'a)
inherit ['hrRep]employee
val name = myName
method receiveEvaluation rep = rep#visitAccountant self
method getName = name
end;;
class ['hrRep] salesman myName = object (self : 'a)
inherit ['hrRep]employee
val name = myName
method receiveEvaluation rep = rep#visitSalesman self
method getName = name
end;;
class virtual ['accountant, 'salesman] hrRep = object (self)
method virtual visitSalesman : 'salesman -> unit
method virtual visitAccountant : 'accountant -> unit
end;;
class ['employee, 'salesman] lowerLevelHRRep =
object (self) inherit ['employee, 'salesman]hrRep
method visitSalesman s = print_endline ("Visiting salesman "^s#getName)
method visitAccountant a =
print_endline ("Visiting accountant "^a#getName)
end;;
let s1 : (<visitSalesman : 'a -> unit>) salesman = new salesman "Bob";;
let a1 : (<visitAccountant : 'a -> unit>) accountant = new accountant "Mary";;
let s2 : (<visitSalesman : 'a -> unit>) salesman = new salesman "Sue";;
let h1 : (<getName : string>, <getName : string>) lowerLevelHRRep = new lowerLevelHRRep;;
s1#receiveEvaluation h1;;
내가 컴파일에 얻을 오류 :
The type of this expression, <visitSalesman : 'a -> unit; _.. > salesman as 'a,
contains type variables that cannot be generalized.
그러나,이 코드는 컴파일 뺀 라인은 salesman
를 인스턴스화.
클래스의 기능을 유지하면서 salesman
을 인스턴스화하는 방법은 무엇입니까?
편집 오류가 receiveEvaluation에 호출을받은 :
This expression has type (<getName:string>, < getName:string>) lowerLevelHRRep
but is here used with type <visitSalesman : 'a salesman -> unit > as 'a.
두 번째 객체 유형은 어떤 방법 visitAccountant
이 없습니다. 초기 컴파일 오류의 해상도, 재귀 솔루션 및 매개 변수화 솔루션 컴파일 에러 코드가 작동
주
해상도 : -
그 컴파일 문제를 해결 한 것,하지만 필자는 세일즈맨 개체의 함수 호출로 컴파일하려고하면 비슷한 문제가 발생합니다. 함수를 호출하려면 어떻게해야합니까? 다시 한 번 감사드립니다! –
이 문제를 이해하고 있는지 확실하지 않습니다. 코드를 게시 할 수 있습니까? 또한 재귀 적 정의를 사용하여보다 단순하지만 (제한적 임에도 불구하고) 솔루션을 추가했습니다. 희망이 도움이됩니다! –
너의 것은 똑같은 일을 성취하기위한 훨씬 더 우아한 해결책이다. 개정 본에서 요청한 내용을 수행 할 수있는 방법이 있다고 생각되면 원래 질문의 코드를 업데이트했습니다. –