{"action":"opened","issue":{"url":"https://api.github.com/repos/gopherjs/vecty/issues/206","repository_url":"https://api.github.com/repos/gopherjs/vecty","labels_url":"https://api.github.com/repos/gopherjs/vecty/issues/206/labels{/name}","comments_url":"https://api.github.com/repos/gopherjs/vecty/issues/206/comments","events_url":"https://api.github.com/repos/gopherjs/vecty/issues/206/events","html_url":"https://github.com/gopherjs/vecty/issues/206","id":327733828,"number":206,"title":"improvements to vecty.If/vecty.MarkupIf","user":{"login":"cj123","id":306873,"avatar_url":"https://avatars0.githubusercontent.com/u/306873?v=4","gravatar_id":"","url":"https://api.github.com/users/cj123","html_url":"https://github.com/cj123","followers_url":"https://api.github.com/users/cj123/followers","following_url":"https://api.github.com/users/cj123/following{/other_user}","gists_url":"https://api.github.com/users/cj123/gists{/gist_id}","starred_url":"https://api.github.com/users/cj123/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cj123/subscriptions","organizations_url":"https://api.github.com/users/cj123/orgs","repos_url":"https://api.github.com/users/cj123/repos","events_url":"https://api.github.com/users/cj123/events{/privacy}","received_events_url":"https://api.github.com/users/cj123/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-05-30T13:35:13Z","updated_at":"2018-05-30T13:35:13Z","closed_at":null,"author_association":"NONE","body":"The current way of using `vecty.If()` and `vecty.MarkupIf()` is unsafe under certain circumstances that a normal `if` would prevent against.\r\n\r\nFor example, take a simple nil check in go:\r\n\r\n```go\r\nvar foo *Something\r\n\r\nif foo != nil {\r\n  // you can safely use foo as a *Something here.\r\n} \r\n```\r\n\r\nthe same principle does not however apply in Vecty.\r\n\r\n```go\r\nvecty.If(foo != nil, foo.Bar) // foo.Bar will cause a panic here if foo is nil\r\n```\r\n\r\nThis is because foo.Bar is evaluated before the method call, regardless of whether foo is nil or not.\r\n\r\nOne way to avoid this would be to make the second parameter to vecty.If a function, e.g.\r\n\r\n```go\r\nfunc If(cond bool, f func() vecty.ComponentOrHTML) vecty.MarkupOrChild {\r\n\tif cond {\r\n\t\treturn f()\r\n\t}\r\n\r\n\treturn nil\r\n}\r\n```\r\n\r\nDoing this would allow for safer uses of vecty.If without requirement of extra checks elsewhere, as the func is only called if the condition is met. \r\n\r\ne.g.\r\n```go\r\nvecty.If(foo != nil, func() vecty.ComponentOrHTML {\r\n    return elem.Div(\r\n         vecty.Text(foo.Bar),\r\n    )\r\n})\r\n```\r\n\r\nI can see however how this is more verbose and perhaps not as flexible as vecty.If's current method signature. Just interested in opening a discussion about how this could be improved."}}
{"actor":{"display_login":"cj123"}}