Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvements to vecty.If/vecty.MarkupIf #206

Open
cj123 opened this issue May 30, 2018 · 0 comments
Open

improvements to vecty.If/vecty.MarkupIf #206

cj123 opened this issue May 30, 2018 · 0 comments

Comments

@cj123
Copy link

cj123 commented May 30, 2018

The current way of using vecty.If() and vecty.MarkupIf() is unsafe under certain circumstances that a normal if would prevent against.

For example, take a simple nil check in go:

var foo *Something

if foo != nil {
  // you can safely use foo as a *Something here.
} 

the same principle does not however apply in Vecty.

vecty.If(foo != nil, foo.Bar) // foo.Bar will cause a panic here if foo is nil

This is because foo.Bar is evaluated before the method call, regardless of whether foo is nil or not.

One way to avoid this would be to make the second parameter to vecty.If a function, e.g.

func If(cond bool, f func() vecty.ComponentOrHTML) vecty.MarkupOrChild {
	if cond {
		return f()
	}

	return nil
}

Doing 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.

e.g.

vecty.If(foo != nil, func() vecty.ComponentOrHTML {
    return elem.Div(
         vecty.Text(foo.Bar),
    )
})

I 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant