Skip to content

Consider adding NullableRef and NullableValue wrappers #610

Closed

Description

@jibbers42

The code below does not fail in v3. This seems similar to #30 (v2 does still return nulls).

[Property]
public Property Test(string s) {
  return (s != null).Collect(s);
}

Is this an expected change for v3? What is the recommended way to have nulls mixed in?

Activity

  1. kurtschelfthout commented on Jun 3, 2022

    @kurtschelfthout
    Member

    Yeah, I felt like the default has changed to "nulls are opt-in" in recent years, in F# since the beginning and in C# more recently.

    You can write a string generator that also generates nulls in whatever frequency you'd like.

  2. jibbers42 commented on Jun 3, 2022

    @jibbers42
    Author

    Thank you, I was able to figure out the following which fails the test with nulls...

    [Property]
    public Property Test() {
      // Gen<string?> NullableString() => Gen.Frequency((1, Gen.Constant<string?>(null)), (7, ArbMap.Default.GeneratorFor<string?>()));
      Gen<string?> NullableString() => Gen.OrNull(ArbMap.Default.GeneratorFor<string?>());
    
      return Prop.ForAll(NullableString().ToArbitrary(), s => {
        return (s != null).Collect(s);
      });
    }

    I see something in the source called Nullable that looks like it gens a null 1/8th of the time and may be simpler to use, but I can't figure out how to use it. Do you have an example somewhere?

  3. kurtschelfthout commented on Jun 3, 2022

    @kurtschelfthout
    Member

    that's in reference to https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1.value?view=net-6.0 i.e. for nullable value types. So it won't help with strings.

    Should probably have NullableRef and NullableValue at this point...

    The idea to use this would be:

    public Property Test(NullableRef<string> wrapper) {
        var theStringOrNull = wrapper.Value;
        
    }
    
  4. jibbers42 commented on Jun 3, 2022

    @jibbers42
    Author

    OK, thanks for all the help!

    Not sure if you want this issue open for "NullableRef and NullableValue", so feel free to close if not.

  5. changed the title [-]No null strings in 3.0.0-beta2 (C#)[/-] [+]Consider adding NullableRef and NullableValue wrappers[/+] on Jun 11, 2022
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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions