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

Better ToString implementation for Result #719

Open
njlr opened this Issue Jan 28, 2019 · 4 comments

Comments

Projects
None yet
3 participants
@njlr
Copy link

njlr commented Jan 28, 2019

Better ToString implementation for Result

I propose we improve the ToString () implementation of Result in order to make it more ergonomic in the REPL.

> Result.Ok "Hello, world. " ;;
val it : Result<string,'a>

> Option.Some "Hello, world. " ;; 
val it : string option = Some "Hello, world. "

As you can see, Option is easy to inspect in the REPL, but Result is not.

Currently, the user needs to do some kind of match or map + mapError to view the contents.

Pros and Cons

The advantages of making this adjustment to F# are:

  • Result becomes easier to use in fsharpi

The disadvantages of making this adjustment to F# are... none that I can think of.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): XS

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this
@cartermp

This comment has been minimized.

Copy link
Member

cartermp commented Jan 28, 2019

Very easy change to make and I don't see a downside to this.

@dsyme

This comment has been minimized.

Copy link
Collaborator

dsyme commented Feb 26, 2019

I would consider this a bug, as sprintf does what you'd expect:

> sprintf "%A" (Result.Ok "Hello, world. ");;
val it : string = "Ok "Hello, world. ""
@cartermp

This comment was marked as outdated.

Copy link
Member

cartermp commented Feb 26, 2019

@njlr Feel free to submit a PR directly to VisualFSharp here: https://github.com/Microsoft/visualfsharp

No need for an RFC 🙂

@dsyme

This comment has been minimized.

Copy link
Collaborator

dsyme commented Feb 26, 2019

Oh I understand.

The problem is that F# considers Ok 3 to be a generic (generalized) value - implemented by a method which returns the value at the appropriate instantiation - and doesn't pretty-print generic values (it would need to type-instantiate them to do that, which could easily be misleading). So for example this prints correctly:

> Result<string,int>.Ok "Hello, world. " ;;
val it : Result<string,int> = Ok "Hello, world. "

So this is really per-spec, or a suggestion to print generic (generalized) values differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.