antlr / stringtemplate4

StringTemplate 4
http://www.stringtemplate.org
Other
956 stars 231 forks source link

Support for java8's Optional #182

Closed thisiscam closed 4 years ago

thisiscam commented 7 years ago

Java 8 introduces Optional which is more idiomatic than using null. Yet the retriever of Optional is named "get", and there seems to be no way in stringtemplate to retrieve the inner value.

patope commented 6 years ago

Workaround is to use model adaptor, but native support would be better

template.registerModelAdaptor(Optional.class, new ObjectModelAdaptor() {
    @Override
    public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
      Optional optional = (Optional) o;
      switch (propertyName) {
        case "get":
          return optional.get();
        case "present":
          return optional.isPresent();
      }
      return super.getProperty(interp, self, o, property, propertyName);
    }
  });
Clashsoft commented 4 years ago

I don't know how feasible this is due to the project in general being limited to Java 6. Perhaps the requirement should be raised to Java 8 now that 6 and 7 are EOL.

parrt commented 4 years ago

I'm worried about how this would change the semantics...and this is no longer my focus so I don't have the time to evaluate this, I'm afraid.