checkBindingType static method

Object? checkBindingType(
  1. Environment env,
  2. STypeAnnotation? typeNode,
  3. Object? value, {
  4. String? describedAs,
})

The value to bind to a written type typeNode, after checking it and applying the one conversion Dart applies here.

Returns the value to bind — normally value unchanged, or its double widening — and throws D4rtTypeError when the value cannot inhabit the annotation. A null typeNode is an unannotated binding (var x), which admits anything.

SCC29 wrote this for parameter binding; SCD63 made the for-each loop variable the second caller. It is deliberately NOT the is predicate (_valueHasType in the visitor): is is a QUESTION, which must answer "no" when it cannot resolve the type, while this is a BINDING CHECK, which must wave through what it cannot resolve — and which has a conversion in it that is has no business performing.

Implementation

static Object? checkBindingType(
  Environment env,
  STypeAnnotation? typeNode,
  Object? value, {
  String? describedAs,
}) {
  final binding = resolveBinding(env, typeNode, describedAs: describedAs);
  return binding == null ? value : binding.bind(env, value);
}