Skip to content

Nullness - box function from FSharp.Core should return nullable type if argument if nullable and non-nullable type with argument is non-nullable. #17730

Closed as not planned

Description

@Lanayx

Repro steps

let y = box 1

Expected behavior

y should be of type obj

Actual behavior

y has type obj | null

Known workarounds

let y = 
    match box 1 with
    | NonNull i -> i
    | Null -> failwith "never fails"

Related information

Provide any related information (optional):

  • Windows 11
  • dotnet 9.0.0-rc.1.24431.7

Activity

  1. added this to the Backlog milestone on Sep 14, 2024
  2. brianrourkeboll commented on Sep 14, 2024

    @brianrourkeboll
    Contributor

    string null returns "", not null, though.

  3. Lanayx commented on Sep 14, 2024

    @Lanayx
    ContributorAuthor

    @brianrourkeboll oh, thanks, didn't realize that! Updating the issue

  4. changed the title [-]Nullness - string and box functions from FSharp.Core should return nullable type if argument if nullable and non-nullable type with argument is non-nullable.[/-] [+]Nullness - box function from FSharp.Core should return nullable type if argument if nullable and non-nullable type with argument is non-nullable.[/+] on Sep 14, 2024
  5. kerams commented on Sep 14, 2024

    @kerams
    Contributor

    box is not constrained to value types, so obj | null is correct too.

  6. Lanayx commented on Sep 14, 2024

    @Lanayx
    ContributorAuthor

    @kerams it's is correct with current implementation, but it's not a desired solution, that's why I converted it from bug to feature request :)

  7. kerams commented on Sep 14, 2024

    @kerams
    Contributor

    It is desired - box can return null:

    > box (null: string);;
    val it: objnull = <null>
    

    Here's what you want:

    let boxv<'a when 'a: struct> (v: 'a) = box v |> Unchecked.nonNull
    
  8. vzarytovskii commented on Sep 14, 2024

    @vzarytovskii
    Member

    Closing per comments, please raise language suggestion for the libraries/language. Currently we can't change non-generic return type of the function based on value.

  9. Lanayx commented on Sep 15, 2024

    @Lanayx
    ContributorAuthor

    @brianrourkeboll I've just figured out that string can return null, so probably type should be changed (or implementation):

    type A() =
        override x.ToString() = null
    let y = A() |> string // y is null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions