Skip to content

Commit

Permalink
#1087. Syntax tests for non-trivial function object computation added
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Sep 14, 2021
1 parent 21e8932 commit 13a6675
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 0 deletions.
37 changes: 37 additions & 0 deletions LanguageFeatures/Constructor-tear-offs/syntax_A03_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2021, 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.

/// @assertion We now allow instantiating function objects, and therefore we do
/// not need to restrict callable objects either.
///
/// The variable initialization above will, after type inference, be
///
/// int Function(int) intId = Id().call<int>;
/// Also, we allow explicitly instantiating a callable object:
///
/// var intId = Id()<int>;
/// is also type-inferred to the same initialization.
///
/// @description Checks callable object invocation with '!'
/// @author sgrekhov@unipro.ru
// SharedOptions=--enable-experiment=constructor-tearoffs
import "../../Utils/expect.dart";

T foo1<T>(T value) => value;

main() {
int Function(int)? f1;
if (2 > 1) {
f1 = foo1.call<int>;
}
Expect.equals(42, f1!(42));

T foo2<T>(T value) => value;
int Function(int)? f2;
if (2 > 1) {
f2 = foo2.call<int>;
}
Expect.equals(42, f2!(42));
}
34 changes: 34 additions & 0 deletions LanguageFeatures/Constructor-tear-offs/syntax_A04_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2021, 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.

/// @assertion This feature introduces syntax to explicitly instantiate a
/// generic function object or tear-off and a type literal for a generic type.
/// The former for consistency with constructor tear-offs, the latter to
/// introduce in-line types without needing a typedef, like we did for in-line
/// function types originally. We introduce both now because they share the same
/// grammar productions.
///
/// Example:
///
/// T id<T>(T value) => value;
/// var idInt = id<int>; // Explicitly instantiated tear-off, saves on function types.
/// // and
/// Type intList = List<int>; // In-line instantiated type literal.
///
/// @description Checks non-trivial computation to obtain function object for
/// explicit generic instantiation
/// @author sgrekhov@unipro.ru
// SharedOptions=--enable-experiment=constructor-tearoffs
import "../../Utils/expect.dart";

T foo1<T>(T value) => value;

main() {
var funcVar1 = foo1;
var funcVarInt1 = foo1<int>;
Expect.equals(42, funcVarInt1(42));
var funcVarInt2 = ( 2 > 1 ? funcVar1 : funcVar1)<int>;
Expect.equals(42, funcVarInt2(42));
}
35 changes: 35 additions & 0 deletions LanguageFeatures/Constructor-tear-offs/syntax_A04_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2021, 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.

/// @assertion This feature introduces syntax to explicitly instantiate a
/// generic function object or tear-off and a type literal for a generic type.
/// The former for consistency with constructor tear-offs, the latter to
/// introduce in-line types without needing a typedef, like we did for in-line
/// function types originally. We introduce both now because they share the same
/// grammar productions.
///
/// Example:
///
/// T id<T>(T value) => value;
/// var idInt = id<int>; // Explicitly instantiated tear-off, saves on function types.
/// // and
/// Type intList = List<int>; // In-line instantiated type literal.
///
/// @description Checks non-trivial computation to obtain function object for
/// explicit generic instantiation
/// @author sgrekhov@unipro.ru
// SharedOptions=--enable-experiment=constructor-tearoffs
import "../../Utils/expect.dart";

T foo<T>(T value) => value;
typedef T Foo<T>(T t);

Foo<T> getFoo1<T>() => foo<T>;
T Function<T>(T) getFoo2() => foo;

main() {
Expect.equals(42, getFoo1<int>()(42));
Expect.equals(42, getFoo2()<int>(42));
}
33 changes: 33 additions & 0 deletions LanguageFeatures/Constructor-tear-offs/syntax_A04_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2021, 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.

/// @assertion This feature introduces syntax to explicitly instantiate a
/// generic function object or tear-off and a type literal for a generic type.
/// The former for consistency with constructor tear-offs, the latter to
/// introduce in-line types without needing a typedef, like we did for in-line
/// function types originally. We introduce both now because they share the same
/// grammar productions.
///
/// Example:
///
/// T id<T>(T value) => value;
/// var idInt = id<int>; // Explicitly instantiated tear-off, saves on function types.
/// // and
/// Type intList = List<int>; // In-line instantiated type literal.
///
/// @description Checks non-trivial computation to obtain function object for
/// explicit generic instantiation
/// @author sgrekhov@unipro.ru
// SharedOptions=--enable-experiment=constructor-tearoffs
import "../../Utils/expect.dart";

class C<Y> {
X foo<X>(X x) => x;
T Function<T>(T) call<Z>(String s) => foo;
}

main() {
Expect.equals(42, C<Object>()<int>("Lily was here")<int>(42));
}

0 comments on commit 13a6675

Please sign in to comment.