Skip to content

Commit 3986e84

Browse files
author
John Messerly
authored
Merge pull request #601 from dart-lang/doc-optional-features
Docs for no-implicit-casts and no-implicit-dynamic
2 parents 409fbcf + 0d7101d commit 3986e84

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

pkg/dev_compiler/doc/STATIC_SAFETY.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,107 @@ code where a field definition in a subclass shadows the field
416416
allocated. Users should prefer explicit getters and setters in such
417417
cases. See [issue 52](https://github.com/dart-lang/dev_compiler/issues/52).
418418

419+
## Optional Features
420+
421+
### Disable implicit casts (experimental)
422+
423+
This is an optional feature of strong mode. It disables implicit down casts. For example:
424+
425+
```dart
426+
main() {
427+
num n = 0.5;
428+
int x = n; // error: invalid assignment
429+
int y = n as int; // ok at compile time, might fail when run
430+
}
431+
```
432+
433+
Casts from `dynamic` must be explicit as well:
434+
435+
```dart
436+
main() {
437+
dynamic d = 'hi';
438+
int x = d; // error: invalid assignment
439+
int y = d as int; // ok at compile time, might fail when run
440+
}
441+
```
442+
443+
This option is experimental and may be changed or removed in the future. Feedback is appreciated! Contact us at our [mailing list](https://groups.google.com/a/dartlang.org/forum/#!forum/dev-compiler).
444+
Try it out in your project by editing .analysis_options:
445+
446+
```yaml
447+
analyzer:
448+
strong-mode:
449+
implicit-casts: False
450+
```
451+
452+
Or pass `--no-implicit-casts` to Dart Analyzer:
453+
454+
```
455+
dartanalyzer --strong --no-implicit-casts my_app.dart
456+
```
457+
458+
### Disable implicit dynamic (experimental)
459+
460+
This is an optional feature of analyzer, intended primarily for use with strong mode's inference.
461+
It rejects implicit uses of `dynamic` that strong mode inference fails to fill in with a concrete type,
462+
ensuring that all types are either successfully inferred or explicitly written. For example:
463+
464+
```dart
465+
main() {
466+
var x; // error: implicit dynamic
467+
var i = 123; // okay, inferred to be `int x`
468+
dynamic y; // okay, declared as dynamic
469+
}
470+
```
471+
472+
This also affects: parameters, return types, fields, creating objects with generic type, generic functions/methods, and
473+
supertypes:
474+
475+
```dart
476+
// error: parameters and return types are implicit dynamic
477+
f(x) => x + 42;
478+
dynamic f(dynamic x) => x + 42; // okay
479+
int f(int x) => x + 42; // okay
480+
481+
class C {
482+
var f; // error: implicit dynamic field
483+
dynamic f; // okay
484+
}
485+
486+
main() {
487+
var x = []; // error: implicit List<dynamic>
488+
var y = [42]; // okay: List<int>
489+
var z = <dynamic>[]; // okay: List<dynamic>
490+
491+
T genericFn<T>() => null;
492+
genericFn(); // error: implicit genericFn<dynamic>
493+
genericFn<dynamic>(); // okay
494+
int x = genericFn(); // okay, inferred genericFn<int>
495+
}
496+
497+
// error: implicit supertype Iterable<dynamic>
498+
class C extends Iterable { /* ... */ }
499+
// okay
500+
class C extends Iterable<dynamic> { /* ... */ }
501+
```
502+
503+
This feature is to prevent accidental use of `dynamic` in code that does not intend to use it.
504+
505+
This option is experimental and may be changed or removed in the future. Feedback is appreciated! Contact us at our [mailing list](https://groups.google.com/a/dartlang.org/forum/#!forum/dev-compiler).
506+
Try it out in your project by editing .analysis_options:
507+
508+
```yaml
509+
analyzer:
510+
strong-mode:
511+
implicit-dynamic: False
512+
```
513+
514+
Or pass `--no-implicit-dynamic` to Dart Analyzer:
515+
516+
```
517+
dartanalyzer --strong --no-implicit-dynamic my_app.dart
518+
```
519+
419520
### Open Items
420521
421522
- Is / As restrictions: Dart's `is` and `as` checks are unsound for certain types

0 commit comments

Comments
 (0)