dart-sdk/runtime/vm/experimental_features.h
Paul Berry a19ae4ee68 Enable the extension types language feature.
This language feature allows the user to declare a static type using
`extension type` syntax, for example:

    extension type IdNumber(int i) {
      operator <(IdNumber other) => i < other.i;
      bool isValid(Some parameters) => ...;
    }

This behaves similarly to a "wrapper" class:

    class IdNumber {
      final int i;
      IdNumber(this.i);
      operator <(IdNumber other) => i < other.i;
      bool isValid(Some parameters) => ...;
    }

However, at runtime, no wrapper objects are created; instead, an
instance of the extension type is represented directly by its
"representation type" (`int` in the above example), and methods like
`isValid` are resolved statically. This gives developers an
abstraction mechanism with the advantage of zero runtime performance
cost, since no extra heap space is required, and no extra instructions
are needed to convert between an extension type and its underlying
representation.

The disadvantage of using extension types as an abstraction mechanism
is that since no wrapper objects are created at runtime, the
abstraction can be bypassed using `dynamic`, runtime casts, or by
"laundering" the object through contravariant generic methods like
`List.add` (which are runtime checked in Dart). For example:

    main() {
      var id = IdNumber(1);
      var list1 = <int>[];
      List<Object> list2 = list1;
      list2.add(id); // OK at compile time because `IdNumber` is a
                     // subtype of `Object`. OK at runtime because
		     // at runtime, `IdNumber` and `int` are
		     // indistinguishable.
      int i = list1[0];
      print(i);
    }

Extension types are expected to be particularly useful for
low-overhead decoding of external data formats (such as JSON), and for
inter-operation with other languages (such as Javascript).

For additional information see the feature specification:
https://github.com/dart-lang/language/blob/main/accepted/future-releases/extension-types/feature-specification.md

Change-Id: I900a3a25dcfc38bfa9c9f9b5b9fa20f362883653
Tested: Standard trybots
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335062
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-11-21 19:14:49 +00:00

44 lines
1.1 KiB
C++

// Copyright (c) 2020, 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.
// NOTE: THIS FILE IS GENERATED. DO NOT EDIT.
//
// Instead modify 'tools/experimental_features.yaml' and run
// 'dart tools/generate_experimental_flags.dart' to update.
#ifndef RUNTIME_VM_EXPERIMENTAL_FEATURES_H_
#define RUNTIME_VM_EXPERIMENTAL_FEATURES_H_
namespace dart {
enum class ExperimentalFeature {
inline_class,
inference_update_2,
sealed_class,
class_modifiers,
records,
patterns,
unnamed_libraries,
inference_update_1,
enhanced_enums,
named_arguments_anywhere,
super_parameters,
constructor_tearoffs,
generic_metadata,
triple_shift,
nonfunction_type_aliases,
non_nullable,
extension_methods,
constant_update_2018,
control_flow_collections,
set_literals,
spread_collections,
};
bool GetExperimentalFeatureDefault(ExperimentalFeature feature);
const char* GetExperimentalFeatureName(ExperimentalFeature feature);
} // namespace dart
#endif // RUNTIME_VM_EXPERIMENTAL_FEATURES_H_