Skip to content

Type error in function invocation (generic type is inferred to Null instead of actual type) #43253

Closed
@arturaz

Description

@arturaz
$ dart --version
Dart SDK version: 2.10.0-7.2.beta (beta) (Mon Aug 17 11:01:01 2020 +0200) on "windows_x64"

Expected output

Some(foo)

Actual output

Unhandled exception:
type '() => Option<String>' is not a subtype of type '() => Option<Null>' of 'ifNone'
#0      Option.orElse (package:zowo_lib/option_bug.dart)
#1      main (package:zowo_lib/option_bug.dart:27:27)
#2      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

Repro code

import 'dart:io';

class Option<A> {
  final A unsafeGet;

  const Option.some(this.unsafeGet);
  const Option.none() : unsafeGet = null;
  @override String toString() => isSome ? "Some($unsafeGet)" : "None";

  bool get isSome => unsafeGet != null;

  Option<A> orElse(Option<A> Function() ifNone) => isSome ? this : ifNone();
}

Option<A> Some<A>(A a) => Option.some(a);

extension MapExts<K, V> on Map<K, V> {
  Option<V> get(K key) => containsKey(key) ? Some(this[key]) : const Option.none();
}

final map = {"foo": "bar"};

void main() {
  final noneStr = map.get("baz");
  final someStr = noneStr.orElse(() => map.get("foo"));
  stdout.write(someStr);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions