Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string can throw for voptions #6782

Closed
ntwilson opened this issue May 20, 2019 · 5 comments
Closed

string can throw for voptions #6782

ntwilson opened this issue May 20, 2019 · 5 comments

Comments

@ntwilson
Copy link

string doesn't seem to have the same support for voptions that it has for options. For both voptions and options, using the .ToString() method can throw: (None.ToString() & ValueNone.ToString()), but string handles None by somewhat elegantly returning an empty string (and has the same behavior for null). When calling string ValueNone though, it throws an exception. Interestingly, string [None] and string [null] both return "[null]", but string [ValueNone] throws an exception as well.
I apologize if this is intended behavior and I just can't find the discussion of it.

Repro steps

  1. open fsi
  2. type string ValueNone;; and witness the exception
  3. type string [ValueNone];; and witness the exception

Expected behavior

I expected it to return "" and "[null]" or some other sensible values.

Actual behavior

It throws exceptions.

Known workarounds

Use sprintf "%A" or pattern match before attempting to call string.

Related information

Windows 10 Pro
.NET Core SDK: 2.2.204
Visual Studio 2019: 16.0.4
fsi is from "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\FSharp\fsi.exe"
(I'm using whatever FSharp.Core comes bundled with fsi)

@amongonz
Copy link

Apparently, the implementation of .ToString() got copied from the Option implementation, which works on the premise that a non-null option can only be Some. This obviously throws when accessing the Value property on ValueNone.

This DebuggerDisplay attribute requires attention too, as it relies on the same premise.

@ntwilson
Copy link
Author

I suspect it might actually be due to the anyToString function. Even with regular options I can do:

> None.ToString();;
System.NullReferenceException: Object reference not set to an instance of an object.
   at <StartupCode$FSI_0002>.$FSI_0002.main@()
Stopped due to error
> string None;;
val it : string = ""

So it has to do with the magic of the string function, and not the .ToString() method.
Indeed, I can see that:

> match box None with
- | null -> "it's null"
- | _ -> "it's not null";;
val it : string = "it's null"

> match box ValueNone with
- | null -> "it's null"
- | _ -> "it's not null";;
val it : string = "it's not null"

so anyToString isn't capturing the fact that ValueNone should be treated as null. I don't know if that makes the difference when a list contains ValueNone though...

@amongonz
Copy link

The reason that's relevant for Option is that None is actually null in disguise, so it gets handled like any other null reference.

That can't possibly apply to ValueOption, as it is a value type. In fact, the correct representation of string ValueNone should be "ValueNone" or similar, not anything related to null.

@ntwilson
Copy link
Author

Oh I understand your point now, thanks for clarifying.

@cartermp
Copy link
Contributor

cartermp commented Oct 8, 2019

Closing in favor of #7693, since I apparently missed this one and I'd like to consolidate

@cartermp cartermp closed this as completed Oct 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants