Created
May 11, 2019 18:50
-
-
Save Pitometsu/b180d522723d2c3ea35dbac8a6880810 to your computer and use it in GitHub Desktop.
OCaml OOP problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class virtual semigroup = object(self : 'self) | |
method virtual private size : int | |
method plus : 'other. (#semigroup as 'other) -> int = fun other -> self#size + other#size | |
end;; | |
Error: The universal type variable 'other cannot be generalized: | |
it escapes its scope. |
But there's still usage problem:
class one = object
inherit S.semigroup
method private size = 1
end;;
class one : object method plus : S.other -> int method private size : int end
class two = object
inherit S.semigroup
method private size = 2
end;;
class two : object method plus : S.other -> int method private size : int end
(new one)#plus { instance = new two };;
Error: This field value has type two which is less general than
'a. #S.semigroup as 'a
Sad, but simpler expression didn't work:
class virtual a = object(self : 'self)
method virtual private size : int
method plus : 'other. (< 'self; .. > as 'other) -> int = fun other -> self#size + other#size
end;;
Error: The type 'self is not an object type
UPD: not type, but type variable, that's why ^^^
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like I found a workaround, isn't it?