You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/07/13 19:08:00 UTC

[jira] [Commented] (GROOVY-4323) Convention for read only properties.

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

Eric Milles commented on GROOVY-4323:
-------------------------------------

The first two options (as described above) are available immediately.  I used a different field name so that "pogo.name = ..." will throw {{ReadOnlyPropertyException}}.

The final modifier option has some stated drawbacks for frameworks.  Are there specific examples of errors or failures using this solution?

Adding an annotation to indicate the property should not support modification has a number of proposals.  "@Final" (since Groovy 4) or "@ReadOnly" or "@NoSetter" or "@Property(readOnly=true)" or ...

Does the {{@Immutable}} transform cover this?  Or can someone describe a scenario where immutable falls short?  Since Groovy 2.5 there may be a way to use {{@PropertyOptions}} to create a meta-annotation that accomplishes the desired effect.

{code:groovy}
class Pogo {
  private String _name
  String getName() { _name }
}
{code}

{code:groovy}
class Pogo {
  final String name
//String getName() { name }
}
{code}

{code:groovy}
class Pogo {
  @ReadOnly String name
//String getName() { name }
//void setName(String name) { throw new ReadOnlyPropertyException('name', getClass()) }
}
{code}

> Convention for read only properties.
> ------------------------------------
>
>                 Key: GROOVY-4323
>                 URL: https://issues.apache.org/jira/browse/GROOVY-4323
>             Project: Groovy
>          Issue Type: New Feature
>          Components: syntax
>            Reporter: Luke Daley
>            Priority: Major
>
> Now that Groovy 1.7 enforces field finality, I am missing having a shortcut for readonly fields.
> It would be nice to have something like:
> {code}
> class T {
>     @Readonly thing
> }
> {code}
> Which would surpress the generation of the setter that Groovy would ordinarily create.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)