Last active
September 26, 2017 17:25
-
-
Save yjbanov/aa2f901d4a824b56107195997cb97085 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Below is a rewrite of the app generated by `flutter create` using a | |
// hypothetical syntax for a Flutter-oriented Dart language extension, | |
// called dx (file extension .dx). | |
// | |
// Key properties of the language: | |
// | |
// - Valid Dart is also valid .dx (start by renaming .dart to .dx). | |
// - Transpiles file-by-file to plain Dart (syntax translation only). | |
// - Start with strong-mode Dart syntax/semantics, then: | |
// - Add Flutter-specific keywords: widget, state, build. | |
// - Add immutable data type keyword: value. | |
// - Widgets and values are: | |
// - Immutable (can we make it deep?). | |
// - Fields are non-nullable by default (enforced by asserts). | |
// - Add Flutter-specific semantics: | |
// - infer setState() | |
// - infer widget. | |
// - Immutable collection literals | |
// - Add argument-initializer blocks (a.k.a. builder syntax). | |
// - No new, elidable const | |
// - Reduce variety of parens (https://goo.gl/gBxJSz). | |
// - Reduce punctuation (optional semi-colons) (https://goo.gl/gBxJSz). | |
// - Reduce `import` boilerplate. | |
import { | |
flutter/material // semi-colons optional, but only inside new syntax | |
} | |
// Old-style imports work too, but discouraged: | |
import 'package:flutter/foundation.dart'; | |
/// A stateless widget. | |
widget MyApp { | |
build { | |
MaterialApp { | |
// := means assign to named parameter of enclosing constructor call. | |
title := 'Flutter Demo' | |
theme := ThemeData { | |
primarySwatch := Colors.blue | |
} | |
home := MyHomePage { | |
title := 'Flutter Demo Home Page' | |
countFrom := 5 | |
} | |
} | |
} | |
} | |
/// A stateful widget. | |
widget MyHomePage { | |
Key? key -> super.key // open to ideas here | |
String? title = 'Default value' | |
int countFrom | |
// Properties inside state are mutable. | |
state { | |
int _counter = countFrom | |
} | |
build { | |
Scaffold { | |
appBar := AppBar { | |
// widget.title inferred lexically. | |
title := Text(title) | |
} | |
body := Center { | |
child := Column { | |
mainAxisAlignment := MainAxisAlignment.center | |
children := [ | |
Text('You have pushed the button this many times:'), | |
Text('${_counter}') { | |
style := Theme.of(context).textTheme.display1 | |
}, | |
] | |
} | |
} | |
floatingActionButton := FloatingActionButton { | |
onPressed := () { | |
// `setState(() { ... })` inferred lexically from `state.` | |
state._counter++ | |
} | |
tooltip := 'Increment' | |
child := Icon(Icons.add) | |
} | |
} | |
} | |
} | |
/// A value type is: | |
/// | |
/// - Immutable: | |
/// - No setters. | |
/// - How easy is it to enforce deep immutability? | |
/// - NNBD. | |
/// - Has constructor with named parameters matching fields. | |
/// - Has auto-generated hashCode/==. | |
/// - Has auto-generated copy constructor. | |
value Employee { | |
String firstName | |
String? lastName | |
DateTime dateOfBirth | |
int get age => DateTime.now().difference(dateOfBirth).inDays / 365 | |
String get fullName => '$firstName $lastName' | |
} | |
// Plain-old Dart works: | |
Employee updateLastName(Employee original, String newLastName) => original.copy( | |
lastName: newLastName, | |
); | |
class HumanResourcesService { | |
final List<Employee> _employees = <Employee>[]; | |
void add(Employee employee) { | |
_employees.add(employee); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment