Skip to content

Commit

Permalink
[dart mdoe] Fix code example to compile and run with modern Dart vers…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
parlough committed Sep 10, 2023
1 parent ee6a1d2 commit 53faa33
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions mode/dart/index.html
Expand Up @@ -29,33 +29,33 @@ <h2>Dart mode</h2>
import 'dart:math' show Random;

void main() {
print(new Die(n: 12).roll());
print(Die(n: 12).roll());
}

// Define a class.
class Die {
// Define a class variable.
static Random shaker = new Random();
static final Random shaker = Random();

// Define instance variables.
int sides, value;

// Define a method using shorthand syntax.
String toString() => '$value';
final int sides;
int? lastRoll;

// Define a constructor.
Die({int n: 6}) {
if (4 <= n && n <= 20) {
sides = n;
} else {
Die({int n = 6}) : sides = n {
if (4 > n || n > 20) {
// Support for errors and exceptions.
throw new ArgumentError(/* */);
throw ArgumentError(/* */);
}
}

// Define a method using shorthand syntax.
@override
String toString() => '$lastRoll';

// Define an instance method.
int roll() {
return value = shaker.nextInt(sides) + 1;
return lastRoll = shaker.nextInt(sides) + 1;
}
}
</textarea>
Expand Down

0 comments on commit 53faa33

Please sign in to comment.