Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polymorphic instance variable in object ? #8074

Open
vicuna opened this Issue Mar 19, 2003 · 3 comments

Comments

Projects
None yet
1 participant
@vicuna
Copy link
Collaborator

commented Mar 19, 2003

Original bug ID: 1607
Reporter: administrator
Status: acknowledged
Resolution: open
Priority: normal
Severity: feature
Category: typing

Bug description

first, a feature which : could it be possible to have polymorphic
instance variable ?

trying to pass this restriction, I fall in the 2 following bug
(CVS version) :

class type ct = object ('s)
  method fold : ('b -> 's -> 'b) -> 'b -> 'b
end

type t = {f : 'a 'b. ('b -> (#ct as 'a) -> 'b) -> 'b}

(* type checker crash *)

class type ct = object ('s)
  method fold : ('b -> ct -> 'b) -> 'b -> 'b
end

type t = { mutable f : 'b. ('b -> ct -> 'b) -> 'b -> 'b }

class c = object (s: #ct)
  method fold = failwith "undefined" 
  (* OK, thanks to the relaxed value restriction *)
end

class c2 = object (self: #ct)
  val v = {f = failwith "undefined"}  (* bug ? *)
  method fold = v.f
  (* initializer v.f <- ...some code using self... *)
end

This field value has type ('a -> ct -> 'a) -> 'a -> 'a
which is less general than 'b. ('b -> ct -> 'b) -> 'b -> 'b
@vicuna

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 20, 2003

Comment author: administrator

From: lvibert@irisa.fr

first, a feature which : could it be possible to have polymorphic
instance variable ?

Not trivial: one can redefine instance variables with {< v = ... >},
so this can only be done explicitly, the same way as record fields.
But why not.

trying to pass this restriction, I fall in the 2 following bug
(CVS version) :

class type ct = object ('s)
method fold : ('b -> 's -> 'b) -> 'b -> 'b
end

type t = {f : 'a 'b. ('b -> (#ct as 'a) -> 'b) -> 'b}
(* type checker crash *)
Looks like an infinite loop. I shall investigate it.

class type ct = object ('s)
method fold : ('b -> ct -> 'b) -> 'b -> 'b
end

type t = { mutable f : 'b. ('b -> ct -> 'b) -> 'b -> 'b }

class c = object (s: #ct)
method fold = failwith "undefined"
(* OK, thanks to the relaxed value restriction *)
end

There is no relation with the relaxed value restriction: a method body
is a value, this is a function implicitely taking self as parameter.

class c2 = object (self: #ct)
val v = {f = failwith "undefined"} (* bug ? )
method fold = v.f
(
initializer v.f <- ...some code using self... *)
end

This field value has type ('a -> ct -> 'a) -> 'a -> 'a
which is less general than 'b. ('b -> ct -> 'b) -> 'b -> 'b

Interesting interaction of features: with the relaxed value
restriction, [failwith "undefined"] can indeed be given the type
\forall 'a. 'a; however, in order to catch errors earlier, the type
checker first unifies the type of [failwith "unified"] with a
monomorphic instance of the expected type, before attempting
generalization. And since this type contains contravariant variables,
it is not generalizable anymore.

This can indeed be seen a bug, but I'm afraid you cannot write any
meaningful program using such a feature: [failwith "undefined"] will
be called immediately, and you will not be able to construct any
object of this class anyway.

If typing were the only problem, there is an easy workaround:
class c2 =
let dummy = failwith "undefined" in
object (self : #ct)
val v = { f = dummy }
method fold = v.f
end;;
It is typable, but generates an exception at class definition time.

The correct way to do it would be
class c2 = object (self: #ct)
val v = {f = fun _ -> failwith "undefined"}
method fold = v.f
end;;
which already types correctly.

  Jacques
@vicuna

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 26, 2003

Comment author: administrator

From: lvibert@irisa.fr

first, a feature which : could it be possible to have polymorphic
instance variable ?

(Aux developpeurs)
Quelqu'un a une opinion sur le sujet?
Les variables d'instance ressemblant beaucoup a` des champs
d'enregistrement, on peut les traiter exactement de la meme facon pour
le polymorphisme. Est-ce que ca pose un probleme?

trying to pass this restriction, I fall in the 2 following bug
(CVS version) :

class type ct = object ('s)
method fold : ('b -> 's -> 'b) -> 'b -> 'b
end

type t = {f : 'a 'b. ('b -> (#ct as 'a) -> 'b) -> 'b}

(* type checker crash *)

C'est maintenant corrige' en CVS.
Ca m'a aide' a` corriger un autre bug, merci.

Jacques

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 4, 2004

Comment author: administrator

Crash fixed by JG (2003-03-26).
There is a wish too (polymorphic instance variables)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.