ui icon indicating copy to clipboard operation
ui copied to clipboard

LensInto throws exception if key is not found

Open amieres opened this issue 7 years ago • 2 comments

The following code works as long as current is equal to "a" or "b":

let snippets    = ListModel.Create id [ "a" ; "b" ]
let current     = Var.Create "a"
let currentOf k = snippets.LensInto id (fun s n -> n) k

div [
        Doc.Input [] current                                                     
        current.View |> Doc.BindView (fun cur ->  Doc.Input [] (currentOf cur))  
] |> Doc.RunById "tasks"

... when current is different it throws:

WebSharper: Uncaught asynchronous exception TypeError: Cannot read property '$0' of null
    at a (WebSharper.UI.Next.js:1961)
    at WebSharper.UI.Next.js:849
    at Object.Snap.When (WebSharper.UI.Next.js:957)
    at Object.Snap.Map (WebSharper.UI.Next.js:844)
    at Object.o (WebSharper.UI.Next.js:1581)
    at WebSharper.UI.Next.js:1595
    at Object.o (WebSharper.UI.Next.js:1580)
    at WebSharper.UI.Next.js:1595
    at Object.o (WebSharper.UI.Next.js:1580)
    at WebSharper.UI.Next.js:1595

amieres avatar Jun 14 '17 06:06 amieres

Yes it would be useful to provide partial lenses, ie. lenses that return an IRef<option<'T>>.

Tarmil avatar Jun 15 '17 19:06 Tarmil

An alternative is to be able to provide a default value

LensInto default id (fun s n -> n) k

amieres avatar Jul 06 '18 10:07 amieres