Can I use Anonymous Records in transformations?
Closed this issue · 9 comments
I want to start on #295 and other issues.
Can I depend on Anonymous Records?
I'd also propose to change the representation of object literals from interfaces to Anonymous Records.
The problem I see with Anonymous records is that we don't have (yet?) good generation for them in the IDE and intellisense don't really help for creating them.
Also, anonymous records will for the user to provide all the properties while in general in JavaScrip you only set the properties that need to be set. Unless you are not speaking about using them for XXXOptions representation?
Also, anonymous records will for the user to provide all the properties while in general in JavaScrip you only set the properties that need to be set. Unless you are not speaking about using them for XXXOptions representation?
Yeah that's a bit annoying, I think there was a F# proposal to allow to omit these.
But I think the situation is the same with the current interface representation?
But I think the situation is the same with the current interface representation?
No, because you use them like that:
module Thoth.RandomUser
open Fable.Core
open Fable.Core.JsInterop
type MyOption =
abstract Property1 : string with get, set
abstract Property2 : string with get, set
abstract Property3 : string with get, set
let x =
jsOptions<MyOption> (fun o ->
o.Property1 <- "value1"
o.Property3 <- "value3"
)This generates:
export const x = {
Property1: "value1",
Property3: "value3"
};Oh I forgot that the properties need to be mutable in many cases, then it won't work anyway because I think AnonRecords are always immutable.
Edit: there is a proposal to allow this: fsharp/fslang-suggestions#732
I think the same trick would work for AnonRecs:
let fillAnonRec<'T> (f:'T->'T) =
let empty : 'T = !! obj()
f( empty )
let f (x : {| X : int option; Y : string option |}) = ()
f (fillAnonRec(fun o -> {| o with X = Some 1 |}))Hum the things is that jsOptions is optimised by Fable I am not sure about anonymous record in this case.
We need to check the generated JavaScript and also check if there is type info generated of not (in this case we don't want them).
If this pattern becomes common then I'm sure Alfonso will be open to adding an optimization.
My only concern currently is mutable Vs immutable.
Another difference to take into account is that anonymous records should have structural equality by default even when boxed. Anonymous records created Fable will inherit the Record prototype but this won't be the case for objects coming from JS that are signed as records.
So you mean it wouldn't be a good idea to represent interfaces or object literals from TS in Fable as Anon Records ...
Oh well, that sucks a bit, but I guess the current representation using interfaces isn't too bad.
Thank you.