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
Allow functional update in initializer #5223
Comments
Comment author: @garrigue You raise an interesting question. Note that you can already change the internal state of an object when inheriting, just class c = object val x = 1 method get = x end Currently it is refused: Allowing it would mean that the field definition in c overrides the x from |
Comment author: khooyp I wouldn't mind a different syntax to discriminate unit initializers and self initializers, say initializer vs. initializer!. Allowing instance variable definitions to refer to previous definitions doesn't quite cover the use case I described - sharing code in methods - since the definition cannot refer to self (unless self is treated as an instance variable too?). To give a more concrete example, a set class might be defined as: class ['elt] set init_list = object (self : 'self) Furthermore, initializer currently allow mutable objects to be updated, which can already lead to the situation you describe (depending on the order of inherit/initializer). While it may potentially be confusing, but it's also quite convenient. It would be nice to give functional objects similar capabilities. |
Comment author: @mshinwell @garrigue: Are we interested in this proposal or shall we close the issue? It's been five years now... |
Comment author: @garrigue This is asking for a new feature, whose semantics is not so easy to define. |
Original bug ID: 5223
Reporter: khooyp
Assigned to: @garrigue
Status: closed (set by @garrigue on 2016-12-13T00:27:20Z)
Resolution: won't fix
Priority: normal
Severity: feature
Version: 3.12.0
Category: ~DO NOT USE (was: OCaml general)
Bug description
Is it possible to allow functional updates "{< ... >}" in object initializers (or another keyword for such cases)? I.e., allow initializers to return objects of self type? Initializers are quite useful for sharing code between constructors and methods, but it can't be used with functional objects (without making instance variables mutable).
For example:
class c = object (self)
val x = 0
method foo = {< x = ... some complex operation ... >}
initializer
self#foo
end
The text was updated successfully, but these errors were encountered: