-
Notifications
You must be signed in to change notification settings - Fork 207
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
Sealed functions #758
Comments
You need to define "immutable values" somehow. If it means only accessing Can instance methods be sealed? Must overriding methods in subclasses also be sealed? Must classes implementing the interface also make the member sealed? (In which case, they cannot be implemented using (Apart from that, |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Sealability definition A specification and explanation of the implications from the idea proposed at the beginning of the issue. Conventions:
i. DefinitionsA. Sealed views (SV) (inspired by #125, #225)All types have a sealed view (SV) subset interface that corresponds to the methods that fulfill two conditions:
By definition:
The declaration of a sealed view must be explicit, it could either be a sub-interface denoted by some symbol or a sealed keyword that adds to final, var, const. Side note (detail about sealed views as parameters, may be skipped) That consideration is a must at least with the Side note (may be skipped) B. Sealed functions (SF) (inspired by #704)A sealed function fulfills these conditions:
Convenient classifications of sealed functions:
Sealed functions should be explicitly declared, because it makes the intention of an interface transparent, and when overriding methods sealability should be respected or constrained. If sealability of functions is implicit and a child class is overriding methods with a non sealed body, how should the analyzer proceed if all methods of the parent class are unintentionally sealed? Side note (may be skipped)
Enforcing those constraints is very useful as a declaration of intention, it makes their existence meaningful, because otherwise they are purely cosmetic features. ii. Implicationsa. The scope of effect in the state of the program from a sealed function must be a subset (inclusive) from the scope of effect of the function that invoked it. Setters are not reachable through SVs (A.4) and SVs return only SVs (A.5). References to objects created inside an SF are lost when the function finishes executing if it can only access external references as SVs (B.3). The only way a sealed function can have access to an external reference that can be written is by receiving it as parameter (B.2), but that parameter came from the function that invoked it. b. A strong sealed function cannot modify the state of the program A strong sealed function does not receive any regular reference as input parameter, therefore it has no scope of effect in the state of the program (no external setters are reachable) and neither do the sealed function invocations from it, because they must have a subset of the scope of effect of the root sealed function invocation (a). iii. ApplicationsComputed constants: A strong sealed function Communication between isolates #125: Sealed views are a natural way to share values from one isolate to another, because the receiver of the sealed view cannot perform any mutations through it. Sealed views are not immutable but it is guaranteed that only the isolate that created it can perform mutations of values that can be reached through the view. This could be exploited positively by creating special interfaces on top of it, given that the sealed view effectively opens a channel of communication from one isolate to another. Immutability rules of certain fields like final fields still apply. In #225 there is a proposal to make freezable objects which is symbiotic with the idea of passing objects from one isolate to another since the receiver of a view could check for frozen objects and know that their shallow state is not going to change. Initialization: If besides the invocation by declarations of computed constant values it was allowed to invoke strongly sealed functions directly in the source files, it would be possible to perform constant assertions #625. Strictly speaking constant assertions can be performed during the computation of a constant, but it’s more readable and elegant to be able to declare a specialized function for it. Sealed functions cannot write vars outside of their function scope (B.3), but what if static non-nullable types were allowed to be declared without a value and these vars were the exception that sealed functions could write only if they are uninitialized (when a non-nullable is null)? If at the end of initialization any non-nullable vars were null, it would be an error. This implies that if a non-nullable static var is declared without value it must be initialized, which is informative and it is also ensured that it is not initialized twice, because that would also produce an error. Function contracts: Sealed views serve as a great way of documenting that a var and all values retrieved from it (in a deep sense) are to be used in read-only operations. |
Functions declared sealed are constrained to:
The introduction of sealed functions makes it possible to define computed constants.
The text was updated successfully, but these errors were encountered: