How to change F# Interactive newline character

2019-05-14 07:13发布

问题:

In a .fs file a newline is denoted by \r\n, but in the F# Interactive window it is \n.

In a problem I'm currently trying to solve, the length of a multiple line literal string matters. So a problem arises when I am testing code in the F# Interactive window, because the length of the string is different from in normal execution.

I hope there is an option to change the newline 'character' in F# Interactive to \r\n, but I can't find it. Does anyone know where I can achieve this, or some other workaround?

回答1:

You can use conditional compilation to handle this:

#if INTERACTIVE
  text.Replace("\n", System.Environment.NewLine)
#endif

I don't know of a way to change it in fsi. Another option would be to remove, or normalize, the newlines regardless of the execution environment. If the exact length is that important, it might be good to do anyway.

EDIT

If the newlines are only there for readability, you can end each line with a backslash. The backslash, newline, and leading whitespace on the following line are removed at compile time.

let text = "a\
            b"
printfn "%s" text //"ab"

This works the same in VS and FSI. I'm assuming you're sending bits of code to FSI via Alt+Enter or Alt+'.