<commit_before># Dart Language and Library Newsletter
2017-10-06
@floitschG

Welcome to the Dart Language and Library Newsletter.

## Did You Know?
### Static Initializers
This section discusses initializers of static variables. For example, the following program has 3 static variables (`seenIds`, `someDouble` and `A._hashIdCounter`) that are all initialized with a value:

``` dart
import 'dart:math';

final seenIds = new Set<int>();
var someDouble = sin(0.5);
class A {
  static int _hashIdCounter = 0;
  A();
  final hashCode = _hashIdCounter++;
}

main() {
  print(new A().hashCode);  // => 0.
  print(new A().hashCode);  // => 1.
  print(someDouble);  // => 0.479425538604203.
  print(seenIds);  // => {}.
}
```
This program is pretty straightforward and its output should not surprise anyone.

Things get more interesting when the initializers have side effects:

``` dart
int _counter = 0;
int someInt() => _counter++;

var foo = someInt();
final bar = someInt();

class A {
  static int gee = someInt();
}

main() {
  print("A.gee: ${A.gee}");
  print("foo: $foo");
  print("bar: $bar");
}
```
The initializers of `foo`, `bar` and `A.gee` all call `someInt` which has a side-effect of updating the `_counter` variable (which, itself, is initialized with a side-effect-free value: `0`).

The output of this program is:
```
A.gee: 0
foo: 1
bar: 2
```
Dart simply evaluates the initial value at first access. This is a consequence of one of Dart's fundamental properties: *no* code is executed before entering `main`. Every code is run as the consequence of the program's actions.

This choice has many nice properties. For example, loading additional classes doesn't slow down a program. Instantiating an instance of a class will not initialize the static members of that class, and the instantiation is thus very fast.

Conceptually, lazy initialization is done with a getter that checks if the field has already been initialized. If not, it evaluates the initializer expression and sets the value of the field first. That's, at least, the simple version. There is much more that the getter needs to handle.

#### Some Interesting Questions

*What happens, if the field is assigned to before the first reading access to it?*
``` dart
int foo() { throw "bad?"; }
var x = foo();

main() {
  x = 0;
  print(x);
}
```
This is allowed and prints `0`. The initializer expression is simply ignored.

*What happens when the initializer tries to read the field that is currently initialized?*

``` dart
var x = foo();
int foo() => x + 1;

main() { print(x); }
```
Dart requires implementations to throw an exception in this case:
```
Unhandled exception:
Reading static variable 'x' during its initialization
```

However, initializers are allowed to read the field if it has been assigned to first:
``` dart
int foo() {
  x = 0;
  return x + 1;
}

var x = foo();

main() { print(x); }  // => 1.
```<commit_msg>Fix typos in last newsletter.<commit_after># Dart Language and Library Newsletter
2017-10-06
@floitschG

Welcome to the Dart Language and Library Newsletter.

## Did You Know?
### Static Initializers
This section discusses initializers of static variables. For example, the following program has 3 static variables (`seenIds`, `someDouble` and `A._hashIdCounter`) that are all initialized with a value:

``` dart
import 'dart:math';

final seenIds = new Set<int>();
var someDouble = sin(0.5);
class A {
  static int _hashIdCounter = 0;
  A();
  final hashCode = _hashIdCounter++;
}

main() {
  print(new A().hashCode);  // => 0.
  print(new A().hashCode);  // => 1.
  print(someDouble);  // => 0.479425538604203.
  print(seenIds);  // => {}.
}
```
This program is pretty straightforward and its output should not surprise anyone.

Things get more interesting when the initializers have side effects:

``` dart
int _counter = 0;
int someInt() => _counter++;

var foo = someInt();
final bar = someInt();

class A {
  static int gee = someInt();
}

main() {
  print("A.gee: ${A.gee}");
  print("foo: $foo");
  print("bar: $bar");
}
```
The initializers of `foo`, `bar` and `A.gee` all call `someInt` which has a side-effect of updating the `_counter` variable (which, itself, is initialized with a side-effect-free value: `0`).

The output of this program is:
```
A.gee: 0
foo: 1
bar: 2
```
Dart simply evaluates the initial value at first access. This is a consequence of one of Dart's fundamental properties: *no* code is executed before entering `main`. All code is run as the consequence of the program's actions.

This choice has many nice properties. For example, loading additional classes doesn't slow down a program. Instantiating an instance of a class will not initialize the static members of that class, and the instantiation is thus very fast.

Conceptually, lazy initialization is done with a getter that checks if the field has already been initialized. If not, it evaluates the initializer expression and sets the value of the field first. That's, at least, the simple version. There is much more that the getter needs to handle.

#### Some Interesting Questions

*What happens, if the field is assigned to before the first reading access to it?*
``` dart
int foo() { throw "bad?"; }
var x = foo();

main() {
  x = 0;
  print(x);
}
```
This is allowed and prints `0`. The initializer expression is simply ignored.

*What happens when the initializer tries to read the field that is currently being initialized?*

``` dart
var x = foo();
int foo() => x + 1;

main() { print(x); }
```
Dart requires implementations to throw an exception in this case:
```
Unhandled exception:
Reading static variable 'x' during its initialization
```

However, initializers are allowed to read the field if it has been assigned to first:
``` dart
int foo() {
  x = 0;
  return x + 1;
}

var x = foo();

main() { print(x); }  // => 1.
```
{'fraction_non_alphanumeric': 0.11310746064339494, 'fraction_numerical': 0.01403148528405202, 'mean_word_length': 4.122699386503068, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2331657', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1680, 'n_words': 811}