Skip to content

Type inference of function return value #33137

Closed
@pd4d10

Description

@pd4d10
  • Dart SDK Version: 2.0.0-dev.43.0
  • macOS

Considering a simple function like this:

add(int a, int b) {
  return a + b;
}

VSCode(with Dart extension) tells me the return type of add function is dynamic:

add(int a, int b) → dynamic

But if we add a wrong type, like

String add(int a, int b) {
  return a + b;
}

Then it shows an error:

[dart] The return type 'int' isn't a 'String', as defined by the method 'add'.

Obviously the compiler knows the return value is a int, but if we don't declare it explicitly, it will be a dynamic. Is it by design?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions