Skip to content

In a function, non-nullable optional parameters with default value should allow to be called with nullable values #1639

Open
@ramsestom

Description

@ramsestom

I am trying to migrate a (large) flutter app to null sound safety and I encountered an issue on some functions where the dart control flow analyzer is complaining if I try to assign a nullable value to a non nullable optional parameter with a default value

For exemple, I have a function defined like this:
static Vector pixelsToWorld(Vector xyz, Matrix4 pixelUnprojectionMatrix, [double targetZ=0])
So the targetZ parametter is optional and default to 0
The problem is I have some objects that have a targetZ nullable property (double?) and that have to keep this as it is (because I want to be able to differentiate objects that have some information on this targetZ property (targetZ can have a 0 value or something else in this case) from those that don't (in that case the targetZ property would be null)).
When I try to call my pixelsToWorld function from one of these objects, the flow analyzer is complaining that The argument type 'double?' can't be assigned to the parameter type 'double'
So, if I want to get rid of this error, I have to test if my targetZ value is null or not before calling the pixelsToWorld function without or with this optional parameter, which is not really convenient...

Wouldn't it make more sense to automatically attribute an optional parametter with a default value its default value if the function is called with a null value for it? (alter all that is what default parameter values are meant for, assign a default value in case of a missing information on the value of this parameter, be it because of an implicit or explicit null)

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