Static initialization: Difference between revisions

From triplescripts.org wiki
Content added Content deleted
(Created page with "The triple script dialect restricts the number of programming constructs that may appear in a module's top-level scope. Aside from definition of the exported clas...")
 
(→‎See also: +"Cutting out Static" <https://gbracha.blogspot.com/2008/02/cutting-out-static.html>)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
The [[dialect|triple script dialect]] restricts the number of programming constructs that may appear in a module's top-level scope. Aside from definition of the exported class (or function, etc.), the only other constructs that may appear are simple static assignments.
'''Static initialization''' is not allowed; the [[dialect|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 [https://en.wikipedia.org/wiki/Static_single_assignment_form SSA form] in compiler IR.


A class may benefit from the definition of some enum-like values, for example. Suppose we had a <code>LineWriter</code> class and we wanted to be able to discriminate between DOS-style line separators and Unix-style newlines.
A class may benefit from the definition of some enum-like values, for example. Suppose we had a <code>LineWriter</code> class and we wanted to be able to discriminate between DOS-style line separators and Unix-style newlines.
Line 5: Line 11:
<pre>
<pre>
export class LineWriter {
export class LineWriter {
/* ... class implementation omitted for brevity ... */
/* ... class internals omitted for brevity ... */
}
}


Line 14: Line 20:
The definition of <code>LineWriter.TYPE_CRLF</code> and <code>LineWriter.TYPE_LF</code> is valid here, because it's a numeric literal constant, which is one of the permissible forms of static assignment.
The definition of <code>LineWriter.TYPE_CRLF</code> and <code>LineWriter.TYPE_LF</code> is valid here, because it's a numeric literal constant, which is one of the permissible forms of static assignment.


== See also ==
NB: Not to be confused with [https://en.wikipedia.org/wiki/Static_single_assignment_form SSA form] in compiler IR.
* [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.