manifold-systems / manifold

Manifold is a Java compiler plugin, its features include Metaprogramming, Properties, Extension Methods, Operator Overloading, Templates, a Preprocessor, and more.
http://manifold.systems/
Apache License 2.0
2.42k stars 125 forks source link

[Question] Are Manifold String Templates evaluated lazily? #600

Closed EotT123 closed 5 months ago

EotT123 commented 5 months ago

Are String Templates evaluated eagerly or lazily? When using logging statements with traditional replacements, lazy evaluation is beneficial because the evaluation depends on the logging level.

rsmckinney commented 5 months ago

A String template evaluates eagerly. There is no other way since there is a single expression that must evaluate to a String value.

Of course, you can add your own laziness.

String pet = "dog";
logger.debug("{}", message(()-> "my $pet has fleas"));
. . .
Object message(Supplier<String> msg) {
  return new Object() {
    public String toString() {
      return msg.get();
    }
  };
}

Would be a good idea to add nicer extension methods to your logger class if you did go this route, so you could write:

logger.debug(()-> "my $pet has fleas");
EotT123 commented 5 months ago

Thanks for the suggestion!