* [Caml-list] Constraining mutability of record types with interfaces
@ 2014-02-20 18:52 David Allsopp
2014-02-20 19:30 ` Gabriel Scherer
0 siblings, 1 reply; 2+ messages in thread
From: David Allsopp @ 2014-02-20 18:52 UTC (permalink / raw)
To: OCaml List
Is there a profound reason that I'm missing (possibly to do with
optimisations) why one can't have:
Foo.mli
type foo = {bar : unit}
Foo.ml
type foo = {mutable bar : unit}
i.e. have field mutability being something which can be constrained by an
interface in a sort of equivalent way to private types? I'm guessing, as the
compiler goes to the trouble of explaining the exact error, that there is!
David
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] Constraining mutability of record types with interfaces
2014-02-20 18:52 [Caml-list] Constraining mutability of record types with interfaces David Allsopp
@ 2014-02-20 19:30 ` Gabriel Scherer
0 siblings, 0 replies; 2+ messages in thread
From: Gabriel Scherer @ 2014-02-20 19:30 UTC (permalink / raw)
To: David Allsopp; +Cc: OCaml List
One reason that would be unsound is that the variance would be
incorrectly inferred from the immutable interface as being covariant.
You could then do things such as:
module Ref : sig
type +'a ref = { contents : 'a }
val get : 'a ref -> 'a
val set : 'a ref -> 'a -> unit
end = struct
type 'a ref = { mutable contents : 'a }
let get = (!)
let set = (:=)
end
Having covariant (shared) references is unsound.
Now it would be possible to give a weaker type meaning not "the field
is immutable" but "you're not allowed to mutate the field, while some
other parts of the program may be". This status would not imply
covariance, and would be supertype of both "immutable" and "mutable"
fields -- there is work in that direction in Didier Rémy's record
typing systems in the nineties.
On Thu, Feb 20, 2014 at 7:52 PM, David Allsopp <dra-news@metastack.com> wrote:
> Is there a profound reason that I'm missing (possibly to do with
> optimisations) why one can't have:
>
> Foo.mli
> type foo = {bar : unit}
>
> Foo.ml
> type foo = {mutable bar : unit}
>
> i.e. have field mutability being something which can be constrained by an
> interface in a sort of equivalent way to private types? I'm guessing, as the
> compiler goes to the trouble of explaining the exact error, that there is!
>
>
> David
>
>
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2014-02-20 19:31 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-20 18:52 [Caml-list] Constraining mutability of record types with interfaces David Allsopp
2014-02-20 19:30 ` Gabriel Scherer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox