blob: 66fd9bb47a0722f9d501d8d8a6fd11ef8929b91b [file] [log] [blame]
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// SharedOptions=--enable-experiment=non-nullable
import 'package:expect/expect.dart';
int initCalls = 0;
int init() {
++initCalls;
return 123;
}
class A {
static late final int fieldWithInit = init();
static late final int fieldWithTrivialInit = 123;
static late final int? fieldWithNullInit = null;
static late final int fieldWithNoInit;
}
main() {
Expect.equals(0, initCalls);
Expect.equals(123, A.fieldWithInit);
Expect.equals(123, A.fieldWithTrivialInit);
Expect.equals(null, A.fieldWithNullInit);
Expect.equals(1, initCalls);
Expect.equals(123, A.fieldWithInit);
Expect.equals(123, A.fieldWithTrivialInit);
Expect.equals(null, A.fieldWithNullInit);
Expect.equals(1, initCalls);
Expect.throws<Error>(() => A.fieldWithNoInit);
A.fieldWithNoInit = 123;
Expect.equals(123, A.fieldWithNoInit);
Expect.throws<Error>(() => {A.fieldWithNoInit = 456});
Expect.equals(123, A.fieldWithNoInit);
}