-
Notifications
You must be signed in to change notification settings - Fork 27
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
Omitting else
clause in If
method
#67
Comments
@whisk This is a great point to raise. I had actually already been thinking about multiple cases issue, but hadn't considered just the "if true do this, otherwise omit" case. I can think of a couple ways to handle it with the existing patterns (there is no other chaining in the library so I'm not sure that feels right here)...a couple options could be:
For the two or more case, I had been thinking a switch pattern might be nice. Something like: userRole := "editor"
content := elem.Div(nil,
elem.Switch(userRole,
Case{"admin", elem.Text("Admin Dashboard")},
Case{"editor", elem.Text("Editor Tools")},
Case{"viewer", elem.Text("Viewer Section")},
Default(elem.Text("Unknown Role"))
)
) or userRole := "editor"
content := elem.Div(nil,
elem.Switch(userRole,
Case{"admin", elem.Text("Admin Dashboard")},
Case{"editor", elem.Text("Editor Tools")},
Case{"viewer", elem.Text("Viewer Section")},
Default{elem.Text("Unknown Role")}
)
) Curious to hear your thoughts. What approaches do you like? |
@chasefleming those 2 examples of switch are identical to me.. Both are fine to me. @whisk any thoughts. My only 2 cents here are that very often these auth role / groups are data driven because they are changeable in the System at runtime. This means that your checking against a string from the db ( or whatever your using ).. |
@gedw99 they are almost identical. The only difference is in the I was thinking about it more and while I like trueStr := "true case"
result := If(true, &trueStr, nil) I don't love changing it to that so maybe the best option is Although one more question for everyone: is it odd that you can use Curious to hear thoughts. Thanks for the great discussion! |
@whisk @gedw99 I just thought of one other non-breaking option: we could introduce a |
I say go with whatever works now. On we hook up a db up then we can revisit anything like this .. |
Hi @chasefleming, @gedw99, thanks for sharing your ideas!
So I agree that it could be beneficial. Still It's possible to make
But there is no way for proper compile-time checks unfortunately. This is also true for struct argument patters like this:
So if you forget about "then" or "default" you wouldn't know about. So what are you thoughts? |
Agree its good enough for now..
Strong typing loss is fine. Its reality that at some point you loose it with data binding. In summary, I think it's decent solution. |
Sorry for the slow response. This issue required some thought. It's a tricky one.
@whisk You raised an excellent point about potentially omitting 'then' or 'default' cases. And with the variadic you can't limit the dev from adding too many conditions which may be confusing. With the variadic approach, there's a risk of developers adding too many conditions, which could lead to confusion. Type safety is a key advantage of this library, and we aim to minimize runtime errors. In my opinion, the func Switch[T any](key string, defaultCase Default[T], cases ...Case[T]) T {
// ...logic...
}
// Usage
result := Switch(userRole,
Default{elem.Text("Unknown Role")},
Case{"admin", elem.Text("Admin Dashboard")},
Case{"editor", elem.Text("Editor Tools")},
Case{"viewer", elem.Text("Viewer Section")},
) With this approach, there's always a safe fallback to the default case. However, this doesn't entirely address the issue of having a case for 'if this, otherwise nothing.' While we haven't found a perfect solution yet, introducing Please feel free to share any differing views or additional insights. The more discussion here the better to highlight things maybe not yet considered. |
Is there a test case to look at ? |
Hi @whisk ! There is a similar function in solid-js. Show takes a predicate and returns an element (fallback optional argument): <Show when={state.count > 0} fallback={<div>Loading...</div>}>
<div>My Content</div>
</Show> Regarding the switch-case. Have you looked at the lo package? userRole := "editor"
content := elem.Div(
nil, lo.Switch[string, elem.Node](userRole).
Case("admin", elem.Text("Admin Dashboard")).
Case("editor", elem.Text("Editor Tools")).
Case("viewer", elem.Text("Viewer Section")).
Default(elem.Text("Unknown Role")),
) |
@nikpivkin The Re the package, looking at it makes me wonder how much this library should even implement utility functions for things like conditional rendering vs recommend people use the utility libraries they like and let Separately for earlier in this thread, I've created an issue for the addition of the |
Problem
There are cases when
else
clause in conditional rendering is not needed, but there is no way to omit theelse
argument. This results in somewhat awkward code:Possible solutions
Add new method
Introduce new method like
If
but with onlythen
argument.Pros: readable, simple
Cons: It's difficult to come up with a name though.
Introduce method chaining
So we could write something like:
Pros: readable
Cons: not clear how to handle errors when
then
part is missing; could be a breaking change unless new package or method name is usedThe text was updated successfully, but these errors were encountered: