3

I recently found out it was possible to include final in function parameters.

/// Handler for the footer leading checkbox
void _onCheck(final bool value) {
  setState(() {
    _checked = value;
  });
}

However, this feature is not documented anywhere and it's impossible to search any information regarding this topic.

Since the value being passed to the function was already declared elsewhere and could've used var, what are the impacts of using final in function parameters?

Nato Boram
  • 4,083
  • 6
  • 28
  • 58

1 Answers1

5

It works like declaring any other variable as final - the variable cannot be changed after it has been initialized. A parameter is really just a local variable where the initializing value comes from the caller instead of a local expression.

So here, you would get an error if you write value = false; in the function because value is a final variable. You would get no error if you removed the final.

Other than that, there is no difference.

lrn
  • 64,680
  • 7
  • 105
  • 121
  • 2
    Does the compiler use this information for optimisations? – Wikiwix Apr 13 '22 at 13:26
  • 1
    Most likely not. Any compiler capable of doing optimizations is also fully capable of recognizing that a non-`final` variable is never assigned to. There is no optimization it can do for a `final` local variable that it can't also do for a non-`final` local variable which isn't assigned to anyway. – lrn Apr 13 '22 at 16:53