Fable: make jsOptions sexy!

Created on 19 Dec 2017  路  8Comments  路  Source: fable-compiler/Fable

Hi!
I'm currently using jsOptions like that:

f# let selector = jsOptions<FindRequest>( fun r -> let s = jsOptions<Selector>( fun s -> let op = jsOptions<ConditionOperators>( fun c -> c.``$eq`` <- Some !!data.Name ) s.Item("name") <- !^op ) r.selector <- s )

While it absolutely works it just feels a little bit... hum... Well not really super cool to read. Would you have any advice to make this more sexy? 馃摳

All 8 comments

Sometimes it may be cleaner to add the type annotation to the variable instead the method:

let selector: FindRequest = jsOptions( fun r -> ... )

Also in some situations the compiler can infer the type and you don't need the annotation, for example I think (not 100% sure) that if you assign the options directly to r.selector you shouldn't need the type:

r.selector <- jsOptions(fun s ->
  s.["name"] <- !^jsOptions(fun c ->
    c.``$eq`` <- Some !!data.Name
  )
)

If you want to go the full dynamic way, I sometimes used the % operator as alias of createObj:

let inline (~%) xs = createObj xs

let selector = %[
  "selector" ==> %[
    "name" ==> %[
      "$eq" ==> data.Name
    ]
  ]
]

Or with the other style of placing brackets:

let selector =
    %[ "selector" ==>
        %[ "name" ==>
            %[ "$eq" ==> data.Name ] ] ]

if you assign the options directly to r.selector you shouldn't need the type

Oh that's interesting. I'll try that.

I sometimes used the % operator as alias of createObj

Well that's something I also should try.

thanks @alfonsogarciacaro 馃憤

I confirm this works:

f# let selector = jsOptions<FindRequest>( fun r -> r.selector <- jsOptions<Selector>( fun s -> s.Item("name") <- !^jsOptions<ConditionOperators>( fun c -> c.``$eq`` <- Some !!data.Name ) ) )

But can you remove the generic arguments?

let selector: FindRequest = jsOptions(fun r ->
    r.selector <- jsOptions(fun s ->
      s.["name"] <- !^jsOptions(fun c ->
        c.eq <- Some !!data.Name
      )
    )
  )

I confirm this works:

f# let selector : FindRequest = jsOptions( fun r -> r.selector <- jsOptions( fun s -> s.Item("name") <- !^jsOptions<IConditionOperators>( fun c -> c.eq <- data.Name ) ) )

While this does not work:

f# let selector : FindRequest = jsOptions( fun r -> r.selector <- jsOptions( fun s -> s.Item("name") <- !^jsOptions( fun c -> c.eq <- data.Name ) ) )

Ok, makes sense. The type inference doesn't work in all cases. Thanks a lot for confirming!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stkb picture stkb  路  3Comments

alfonsogarciacaro picture alfonsogarciacaro  路  3Comments

nozzlegear picture nozzlegear  路  3Comments

MangelMaxime picture MangelMaxime  路  3Comments

et1975 picture et1975  路  3Comments