Skip to content

Commit 13c99fc

Browse files
Samples for super-initializer and named arguments anywhere (#137)
* created parameters sample * building super initalizer example * add super initializer tests * extra checks * small corrections * completed examples
1 parent 2cdb3c7 commit 13c99fc

8 files changed

+240
-0
lines changed

parameters/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Files and directories created by pub.
2+
.dart_tool/
3+
.packages
4+
5+
# Conventional directory for build output.
6+
build/
7+
8+
pubspec.lock

parameters/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Function parameters and super-initializer samples
2+
3+
This sample demonstrates Dart's function parameters capabilities,
4+
including named parameters, optional parameters, and the ability to have named
5+
arguments anywhere in the argument list.
6+
7+
As well, the sample demonstrates super-initializer parameters.
8+
9+
These features are part of Dart 2.17. The programs in this folder won't
10+
compile in earlier versions.
11+
12+
## Instructions
13+
14+
Read the source code in the `lib/` directory. The code is split into
15+
separate files according to topics or use cases.

parameters/analysis_options.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:lints/recommended.yaml

parameters/lib/named_parameters.dart

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// This example shows the use of named arguments and parameters.
6+
///
7+
/// See the test under `../test/named_parameters_test.dart` for its use.
8+
9+
/// Function that counts the amount of [items] that match a given [predicate],
10+
/// with the option to skip the first [skip] elements.
11+
///
12+
/// This function has three parameters:
13+
/// - Required positional parameter [predicate]
14+
/// - Required named parameter [items]
15+
/// - Optional named parameter [skip]
16+
int countWhere<T>(
17+
bool Function(T) predicate, {
18+
required Iterable<T> items,
19+
int skip = 0,
20+
}) {
21+
return items.skip(skip).where(predicate).length;
22+
}

parameters/lib/super_initalizer.dart

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// This example shows the use of super-initializer parameters.
6+
///
7+
/// Super-initializer parameters allow to forward constructor parameters
8+
/// to the superclass, avoiding having to write the parameter multiple times
9+
/// in the superclass constructor invocation.
10+
11+
/// This example contains multiple classes representing different types of
12+
/// synthesizers, a musical instrument similar to a piano
13+
/// which can be used to create new sounds.
14+
///
15+
/// All subclasses inherit from the [Synth] class and take advantage of the
16+
/// different super-initializer capabilities.
17+
class Synth {
18+
/// Model name
19+
final String model;
20+
21+
/// Amount of notes that can be played at the same time
22+
final int polyphony;
23+
24+
/// Amount of sound generators, default to 1
25+
final int oscillators;
26+
27+
/// In the class constructor [model] is a positional parameter,
28+
/// while [polyphony] and [oscillators] are named parameters.
29+
Synth(
30+
this.model, {
31+
required this.polyphony,
32+
this.oscillators = 1,
33+
});
34+
35+
@override
36+
String toString() {
37+
return 'Synth $model. Polyphony: $polyphony, oscillators: $oscillators';
38+
}
39+
}
40+
41+
/// This class represents an old vintage synthesizer.
42+
///
43+
/// [VintageSynth] can only have 1 oscillator.
44+
/// [polyphony] is optional and is 1 by default.
45+
class VintageSynth extends Synth {
46+
/// [model] is forwarded to the super constructor.
47+
/// Named parameter [polyphony] is forwarded, with the default value of 1.
48+
VintageSynth(super.model, {super.polyphony = 1});
49+
}
50+
51+
/// This class represents a modern digital synthesizer.
52+
///
53+
/// [DigitalSynth] can only have 1 oscillator.
54+
/// Named parameter [polyphony] is required.
55+
class DigitalSynth extends Synth {
56+
/// [model] is forwarded to the super constructor.
57+
/// Named parameter [polyphony] is forwarded and it is required.
58+
DigitalSynth(super.model, {required super.polyphony});
59+
60+
/// The following constructor declaration would not be possible
61+
/// because [polyphony] is not a positional argument.
62+
///
63+
/// DigitalSynth(super.model, super.polyphony);
64+
}
65+
66+
/// This class represents a complex multi-oscillator synthesizer.
67+
///
68+
/// [MultiOscillator] requires all three parameters.
69+
class MultiOscillatorSynth extends Synth {
70+
/// This constructor has three positional parameters instead of one.
71+
///
72+
/// [model] is forwarded to the super constructor.
73+
/// [polyphony] and [oscillators] positional parameters are then passed to the
74+
/// named parameters in the super constructor.
75+
MultiOscillatorSynth(
76+
super.model,
77+
int polyphony,
78+
int oscillators,
79+
) : super(
80+
polyphony: polyphony,
81+
oscillators: oscillators,
82+
);
83+
}
84+
85+
/// This class represents a synth with a fixed amount
86+
/// of polyphony and oscillators.
87+
///
88+
/// [FixedOscillatorSynth] only requires the positional parameter [model].
89+
class FixedOscillatorSynth extends Synth {
90+
/// [model] is forwarded to the super constructor.
91+
/// [polyphony] and [oscillators] are hardcoded.
92+
FixedOscillatorSynth(super.model)
93+
: super(
94+
polyphony: 1,
95+
oscillators: 3,
96+
);
97+
}

parameters/pubspec.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: parameters
2+
description: Function parameters and super initalizers examples
3+
version: 1.0.0
4+
5+
publish_to: none
6+
7+
environment:
8+
sdk: '>=2.17.0-182.1.beta <3.0.0'
9+
10+
dev_dependencies:
11+
lints: ^1.0.0
12+
test: ^1.16.0
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:parameters/named_parameters.dart';
6+
import 'package:test/test.dart';
7+
8+
/// This example shows the use of named arguments.
9+
///
10+
/// Starting in Dart 2.17, named arguments can be used at any position in the
11+
/// arguments list, instead of only after the positional arguments.
12+
///
13+
/// In the following tests, the method [countWhere] is called
14+
/// with a different argument list order to show this.
15+
void main() {
16+
test('named argument after positional argument', () {
17+
final list = [0, 2, 42, 91];
18+
19+
// `items` named argument appears after the positional argument `predicate`
20+
// Default argument `skip` is not provided
21+
final total = countWhere<int>((item) {
22+
return item % 2 == 0;
23+
}, items: list);
24+
25+
expect(total, 3);
26+
});
27+
28+
test('named argument before positional argument', () {
29+
final list = [0, 2, 42, 91];
30+
31+
// `items` named argument appears before the positional argument `predicate`
32+
// Default argument `skip` is not provided
33+
final total = countWhere<int>(items: list, (item) {
34+
return item % 2 == 0;
35+
});
36+
37+
expect(total, 3);
38+
});
39+
40+
test('positional argument between named arguments', () {
41+
final list = [0, 2, 42, 91];
42+
43+
// positional argument `predicate` appears between
44+
// named arguments `items` and `skip`
45+
final total = countWhere<int>(
46+
items: list,
47+
(item) {
48+
return item % 2 == 0;
49+
},
50+
skip: 1,
51+
);
52+
53+
expect(total, 2);
54+
});
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:parameters/super_initalizer.dart';
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
test('Create multiple instances of class Synth', () {
10+
// Constructor with optional argument, provided
11+
final juno = VintageSynth("Juno", polyphony: 6);
12+
expect(juno.toString(), "Synth Juno. Polyphony: 6, oscilators: 1");
13+
14+
// Constructor with optional argument, not provided
15+
final tb303 = VintageSynth("TB-303");
16+
expect(tb303.toString(), "Synth TB-303. Polyphony: 1, oscilators: 1");
17+
18+
// Constructor with required argument
19+
final dx7 = DigitalSynth("DX7", polyphony: 6);
20+
expect(dx7.toString(), "Synth DX7. Polyphony: 6, oscilators: 1");
21+
22+
// Constructor with required positional arguments
23+
final ob6 = MultiOscillatorSynth("OB6", 5, 3);
24+
expect(ob6.toString(), "Synth OB6. Polyphony: 5, oscilators: 3");
25+
26+
// Constructor with a single required positional arguments
27+
final minimoog = FixedOscillatorSynth("MiniMoog");
28+
expect(minimoog.toString(), "Synth MiniMoog. Polyphony: 1, oscilators: 3");
29+
});
30+
}

0 commit comments

Comments
 (0)