Skip to content

Instantly share code, notes, and snippets.

@minikin
Last active November 20, 2020 08:41
Show Gist options
  • Save minikin/a7f52f327eb241a44fc95c4a7b0720e8 to your computer and use it in GitHub Desktop.
Save minikin/a7f52f327eb241a44fc95c4a7b0720e8 to your computer and use it in GitHub Desktop.
Working with optional fields in Swift vs Dart
class Coffee {
String? _temperature;
void heat() {
_temperature = 'hot';
}
void chill() {
_temperature = 'iced';
}
void checkTemp() {
var temperature = _temperature;
if (temperature != null) {
print('Ready to serve ' + temperature + '!');
}
}
String serve() => _temperature! + ' coffee';
}
struct Coffee {
var temperature: String?
mutating func heat() {
temperature = "hot"
}
mutating func chill() {
temperature = "iced"
}
func checkTemp() {
guard let unwrappedTemperature = temperature else {
print("Temperature is unknown :(")
return
}
print("Ready to serve " + unwrappedTemperature + "!")
}
func serve() -> String {
temperature! + " coffee";
}
}
let myCoffee = Coffee()
myCoffee.checkTemp()
@minikin
Copy link
Author

minikin commented Nov 20, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment