maciejhirsz / kobold

Easy declarative web interfaces.
https://docs.rs/kobold/
Mozilla Public License 2.0
388 stars 7 forks source link

Keywords #38

Closed maciejhirsz closed 1 year ago

maciejhirsz commented 1 year ago

Keywords

This is something I thought I could avoid doing, but with the number of methods and traits such as StrExt grows I feel like the prelude becomes kind of bloated, and it might not be so easy to figure out where those methods come from when traits are imported with use Trait as _;

This PR deprecates the old methods and reintroduces them as keywords that can be used inside the { ... } expressions in the view! macro:

In addition this PR introduces a keyword that currently has no equivalent:

Documentation & Rationale

All of the keywords are implemented as plain functions in a new keywords module:

image

The view! macro might do some extra optimizations, but fundamentally all of these are just function calls using raw identifiers, meaning this:

view! {
    <p>{ static foo }</p>
}

Desugars into:

view! {
    <p>{ kobold::keywords::r#static(foo) }</p>
}

Originally I felt like adding keywords to expressions obfuscated things: using for with iterators makes the view! macro less like ordinary Rust and even more specialized DSL. After adding all the diffing interactions however I now believe keywords organized in a module are actually more discoverable, while not being significantly more DSL-y than trait extension methods.

rust-analyzer also handles this really well:

image

Alternatives considered: