Join GitHub today
Provide universal tuple item accessor functions in FSharp.Core #628
Provide universal tuple item accessor functions in FSharp.Core
As it currently stands,
Tuple-like objects in this context includes System.Tuple, System.ValueTuple and everything that exposes ItemX / Rest.
let tpl = (1,2,3,4) let x = fst tpl let tpl = struct (1,2,3,4,5) let x = fst tpl
Microsoft/visualfsharp#4034 exposes ItemX and Rest on System.Tuple with a warning and hidden from intellisense.
The same would need to be done for System.ValueTuple.
Then the acessor functions can be implemented using SRTP.
The existing way of approaching this problem in F# is ...
Pros and Cons
The advantages of making this adjustment to F# are ...
// current way let (_,_,_,x,_,_,_) = (1,2,3,4,5,6,7) // new way let x = item4 (1,2,3,4,5,6,7)
The disadvantages of making this adjustment to F# are ...
To explicitly respond to
The small advantage of these accessor functions over ItemX / Rest properties is that they are not directly visible in IntelliSense. So if you just hit the dot on a tuple, you still don't see them.
Estimated cost (XS, S, M, L, XL, XXL): S-M
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:
referenced this issue
Nov 29, 2017
Who says you can't already use SRTP? The king returns! ;) Here's a new one for @gusty to appreciate:
#light "off" #nowarn "62";; #r "System.ValueTuple.dll";; type fstsnd = class static member Fst (struct (x, _)) = x static member Fst ((x, _)) = x static member Snd (struct (_, y)) = y static member Snd ((_, y)) = y static member inline _invoke_fst (x : ^a) = let inline call (arg, _ : ^b) = ((^a or ^b) : (static member Fst : _ -> _) arg) in call (x, Unchecked.defaultof<fstsnd>) static member inline _invoke_snd (x : ^a) = let inline call (arg, _ : ^b) = ((^a or ^b) : (static member Snd : _ -> _) arg) in call (x, Unchecked.defaultof<fstsnd>) end let inline xfst x = fstsnd._invoke_fst x let inline xsnd x = fstsnd._invoke_snd x let x = xfst (struct (1, 2)) let y = xfst (3, 4) let xx = xsnd (struct (5, 6)) let yy = xsnd (7, 8)
Guys, you're right, SRTP solve it, though for a limited number of elements.
I have wrote some stuff like that for F#+ but mainly to get lenses for tuples. I agree with @dsyme in that accessing the tuples by properties (or even by