Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Static initialization: Difference between revisions

From triplescripts.org wiki
Content added Content deleted
m (→‎See also: rm extraneous `]`)
(→‎See also: +"Cutting out Static" <https://gbracha.blogspot.com/2008/02/cutting-out-static.html>)
 
Line 22: Line 22:
== See also ==
== See also ==
* [http://www.object-oriented-security.org/lets-argue/singletons Singletons Considered Harmful]
* [http://www.object-oriented-security.org/lets-argue/singletons Singletons Considered Harmful]
* [https://gbracha.blogspot.com/2008/02/cutting-out-static.html Cutting out Static]

Latest revision as of 21:08, 3 August 2022

Static initialization is not allowed; the triple script dialect restricts the number of programming constructs that may appear in a module's top-level scope, including a prohibition on any global state. Singleton instances, therefore, are not possible to create or enforce at the language level and are only the consequence of a failure to exercise the ability of the create multiple instances throughout the lifetime of a given program. It is not possible to bind to a singleton instance by name. Instead, any such use of a "soft singleton" must involve parametric definition.

Simple static assignment[edit]

Aside from definition of the exported class (or function, etc.), the only other constructs that may appear in a top-level scope are simple static assignments. (These are in fact not really assignments, but merely simple definitions themselves—the semantics of the equals sign in these static "assignments" differs from that of ordinary assignments that occur at runtime.)

NB: Not to be confused with SSA form in compiler IR.

A class may benefit from the definition of some enum-like values, for example. Suppose we had a LineWriter class and we wanted to be able to discriminate between DOS-style line separators and Unix-style newlines.

export class LineWriter {
  /* ... class internals omitted for brevity ... */
}

LineWriter.TYPE_CRLF = 0;
LineWriter.TYPE_LF = 1;

The definition of LineWriter.TYPE_CRLF and LineWriter.TYPE_LF is valid here, because it's a numeric literal constant, which is one of the permissible forms of static assignment.

See also[edit]

Cookies help us deliver our services. By using our services, you agree to our use of cookies.