Static initialization: Difference between revisions

From triplescripts.org wiki
(→‎Simple static assignment: wording about not really being assignments)
m (→‎See also: rm extraneous `]`)
Line 21: Line 21:
   
 
== 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]

Revision as of 21:13, 27 May 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

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

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