The points about encapsulation are pointed at immutable fields (which are 
public) and don't hold for mutable fields, which are provided as an 
advanced mechanism for building higher-level constructs.

The deftype docstring says:

  Fields can be qualified
  with the metadata :volatile-mutable true or :unsynchronized-mutable
  true, at which point (set! afield aval) will be supported in method
  bodies. Note well that mutable fields are extremely difficult to use
  correctly, and are present only to facilitate the building of higher
  level constructs, such as Clojure's reference types, in Clojure
  itself. They are for experts only - if the semantics and
  implications of :volatile-mutable or :unsynchronized-mutable are not
  immediately apparent to you, you should not be using them.

As the datatypes reference page also says, "Concrete derivation is bad" - 
this is not something we have any desire to enable or support.

Some other options that usually are better:
- Define interfaces and have the deftype implement them for access
- Use composition instead of inheritance
- Use fields holding Clojure stateful constructs (like atoms)


On Monday, August 27, 2018 at 1:08:21 AM UTC-5, Didier wrote:
>
> It seems to contradict the documentation found here datatypes 
> <https://clojure.org/reference/datatypes#_deftype_and_defrecord>:
>
> > Encapsulation of information is folly. Fields are public, use 
> protocols/interfaces to avoid dependencies
>
> It is also not documented in the doc-string, and makes it impossible to 
> further extend the type. No more ad-hoc polymorphism basically.
>
> So you can't do this:
>
> (deftype MutableBox [^{:volatile-mutable true} contents])
>
> (defprotocol Container
>   (inventory [this]))
>
> (extend-type MutableBox
>   Container
>   (inventory [this] (.-contents this)))
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to