askama icon indicating copy to clipboard operation
askama copied to clipboard

Associated functions on structs with generics are not callable

Open cassie-rust opened this issue 1 year ago • 1 comments

# rust
struct Foo<const BAR: bool> {}

impl<const BAR: bool> Foo<BAR> {
  fn baz() -> String {
    String::from("I should get rendered!")
  }
}

# template
<p>Let's baz!</p>
<h1>{% Foo::<false>::baz() %}</h1>

The above should yield I should get rendered! but instead results in a parsing error:

error: problems parsing template source at row 12, column 50 near:
       "::<false>::baz() %}</h1>"...
   --> src/etc/...
    |
111 | #[derive(Template)]
    |          ^^^^^^^^
    |
    = note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

The workaround for now is to add a method in the template struct and call Foo::<false>::baz() there.

cassie-rust avatar Nov 20 '23 05:11 cassie-rust

Yeah, I'm not sure I'll want to support this, to avoid leaking too much complexity into the template language.

djc avatar Nov 20 '23 08:11 djc

That's certainly a valid call; I mostly opened an issue in case it was an oversight. Thank you for the response. :)

cassie-rust avatar Feb 02 '24 20:02 cassie-rust