Working with strings in F # is sometimes a little inconvenient. I would probably use the same code as Dario. Grammar F # does not allow the use of constructors as functions of the first class, so you, unfortunately, cannot process all the processing in one pipeline. In general, you can use static members and instance methods as functions of the first class, but not instance properties or constructors.
Anyway, there is a very nasty trick that you can use to turn a constructor into a function value. I would not recommend using it, but I was very surprised to see that it really works, so I thought it might be worth sharing it:
let inline ctor< ^R, ^T when ^R : (static member ``.ctor`` : ^T -> ^R)> (arg:^T) = (^R : (static member ``.ctor`` : ^T -> ^R) arg)
This defines the function that will be included at the time of compilation, which requires that the parameter of the first type has a constructor that takes the value of the parameter of the second type. This is indicated as a compilation time limit (since .NET generators cannot express this). In addition, F # does not allow you to specify this using the usual syntax for specifying constructor constraints (which should take unit as an argument), but you can use the compiled name of the constructors. Now you can write, for example:
And you can also use the ctor result as a first-class function:
let chars = [ 'a'; 'b'; 'c' ] let str = chars |> Array.ofSeq |> ctor<System.String, _>
As I said, I think this is mostly curiosity, but quite interesting :-).
source share