[ 
https://issues.apache.org/jira/browse/SLING-8706?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17141790#comment-17141790
 ] 

Tomasz Niedźwiedź commented on SLING-8706:
------------------------------------------

Hi [~joerghoh]. I saw your tweet about this feature. It does look interesting 
but I feel the downsides might outweigh the benefits. As [~sseifert] pointed 
out, {{Optional}} wasn't really designed to be used in fields.

I do see a clear benefit in using {{Optional}} at API boundaries, where another 
party may consume my code. Inside a Sling model, it looks like a bit of an 
ovekill. These kind of classes are usually small, easy to understand and easy 
to control. Existing Sling Models features allow us to provide default values 
for missing fields. The lack of serialisation may not be more than a code smell 
(given how Sling Models are used these days) but it would be interesting to 
assess how using instances of {{Optional}} compares to the use of plain 
{{null}} references in terms of the amount of memory consumed.

Also, how would one go about exposing a field like this in HTL? Would HTL 
understand the Optional:
{code:java}
// HTL expected to treat the Optional as falsy?
public Optional<String> getMyProperty() {
  return myProperty;
} {code}
or would I have to unwrap the {{Optional}}, as in:
{code:java}
public String getMyProperty() {
  return myProperty.orElse("");
}{code}
in which case, I'd actually prefer to use some static utility method or an 
operator and handle a simple null check. Or I could use {{@Default}}.

[~joerghoh] could you provide some examples of models where this approach would 
save the developer some effort/errors? I can't think of a model where this 
would help significantly that wouldn't be solvable via existing Sling features 
and/or refactoring.

> Injections for java.util.Optional<> should be automatic optional 
> -----------------------------------------------------------------
>
>                 Key: SLING-8706
>                 URL: https://issues.apache.org/jira/browse/SLING-8706
>             Project: Sling
>          Issue Type: Improvement
>          Components: Sling Models
>            Reporter: Jörg Hoh
>            Priority: Major
>          Time Spent: 2h 10m
>  Remaining Estimate: 0h
>
> The current approach to support optional injections requires to annotate the 
> field with {{@Optional}} plus proper handling within the javacode (null 
> checks etc), which can be forgotten.
> So instead of
> {code}
> @Inject @Optional
> String fieldname;
> {code}
> it should also be possible to use this
> {code}
> @Inject
> Optional<String> fieldname;
> {code}
> with the very same semantic. But the developer is forced to deal with the 
> case that the value is not present.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to