[ 
https://issues.apache.org/jira/browse/GROOVY-11059?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Jim Klimov updated GROOVY-11059:
--------------------------------
    Description: 
Java has basic casting which relies on one object having a class/interface 
which is descendant of another in the equation. Groovy extends this with 
coercion which relies on the origin class defining an {{asType()}} method to 
state how convert it into an instance of a target class.

For example, if I want to assign my class from a standard {{String}} I'd have 
to hack with metaclass of String to extend its {{asType}} to recognize 
{{MyClass}} and coerce INTO it, and then I'd be able to say:
{code:java}
MyClass myval = "SomeString" as MyClass{code}
This is clumsy both on implementation side (hacking into other people's code is 
not too portable, and has complications with code that runs this hack many 
times like a Jenkins Shared Library that would change server JVM's String over 
and over for each run), and on coding side (while "as SomeClass" is explicit, 
someone has to write it which adds noise in refactorings and goes against the 
type simplicity trend of the language).

My proposal is to support a common optional method everywhere, in the groovyish 
manner of plastering so many other optional operator overloads like 
{{{}asType(){}}}, {{{}isCase(){}}}, {{plus()}} et al, which would be 
effectively an inverse {{asType()}} and overload the assignment operator.

By default it could invoke single-parameter constructors (e.g. if I define a 
{{MyClass(String)}} one - that can be used to cast the assignment from a 
{{{}String{}}}), and it might be extensible if someone has more complex ideas 
for their class (e.g. change something in the existing instance instead of 
creating a new one, if the override is non-static, or if it gets a parameter 
with an instance of this class and this parameter is null or not).

Possibly this might fill in the gap of current {{equals()}} overload that did 
not always work well for me (possibly due to ancient groovy in Jenkins core) to 
check {{{}if (myClassVal == "somestring") {...{}}}}

  was:
Java has basic casting which relies on one object having a class/interface 
which is descendant of another in the equation. Groovy extends this with 
coercion which relies on the origin class defining an {{asType()}} method to 
state how convert it into an instance of a target class.

For example, if I want to assign my class from a standard {{String}} I'd have 
to hack with metaclass of String to extend its {{asType}} to recognize 
{{MyClass}} and coerce INTO it, and then I'd be able to say:
{code:java}
MyClass myval = "SomeString" as MyClass{code}
This is clumsy both on implementation side (hacking into other people's code is 
not too portable, and has complications with code that runs this hack many 
times like a Jenkins Shared Library that would change server JVM's String over 
and over for each run), and on coding side (while "as SomeClass" is explicit, 
someone has to write it which adds noise in refactorings and goes against the 
type simplicity trend of the language).

My proposal is to support a common optional method everywhere, in the groovyish 
manner of plastering so many other optional operator overloads like 
{{{}asType(){}}}, {{{}isCase(){}}}, {{plus()}} et al, which would be 
effectively an inverse {{asType()}} and overload the assignment operator.

By default it could invoke single-parameter constructors (e.g. if I define a 
{{MyClass(String)}} one - that can be used to cast the assignment from a 
{{{}String{}}}, and it might be extensible if someone has more complex ideas 
for their class (e.g. change something in the existing instance instead of 
creating a new one, if the override is non-static, or if it gets a parameter 
with an instance of this class and this parameter is null or not).

Possibly this might fill in the gap of current {{equals()}} overload that did 
not always work well for me (possibly due to ancient groovy in Jenkins core) to 
check {{{}if (myClassVal == "somestring") {...{}}}}


> Want a way to define casting/coercing FROM other classes as part of my class
> ----------------------------------------------------------------------------
>
>                 Key: GROOVY-11059
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11059
>             Project: Groovy
>          Issue Type: Wish
>            Reporter: Jim Klimov
>            Priority: Major
>
> Java has basic casting which relies on one object having a class/interface 
> which is descendant of another in the equation. Groovy extends this with 
> coercion which relies on the origin class defining an {{asType()}} method to 
> state how convert it into an instance of a target class.
> For example, if I want to assign my class from a standard {{String}} I'd have 
> to hack with metaclass of String to extend its {{asType}} to recognize 
> {{MyClass}} and coerce INTO it, and then I'd be able to say:
> {code:java}
> MyClass myval = "SomeString" as MyClass{code}
> This is clumsy both on implementation side (hacking into other people's code 
> is not too portable, and has complications with code that runs this hack many 
> times like a Jenkins Shared Library that would change server JVM's String 
> over and over for each run), and on coding side (while "as SomeClass" is 
> explicit, someone has to write it which adds noise in refactorings and goes 
> against the type simplicity trend of the language).
> My proposal is to support a common optional method everywhere, in the 
> groovyish manner of plastering so many other optional operator overloads like 
> {{{}asType(){}}}, {{{}isCase(){}}}, {{plus()}} et al, which would be 
> effectively an inverse {{asType()}} and overload the assignment operator.
> By default it could invoke single-parameter constructors (e.g. if I define a 
> {{MyClass(String)}} one - that can be used to cast the assignment from a 
> {{{}String{}}}), and it might be extensible if someone has more complex ideas 
> for their class (e.g. change something in the existing instance instead of 
> creating a new one, if the override is non-static, or if it gets a parameter 
> with an instance of this class and this parameter is null or not).
> Possibly this might fill in the gap of current {{equals()}} overload that did 
> not always work well for me (possibly due to ancient groovy in Jenkins core) 
> to check {{{}if (myClassVal == "somestring") {...{}}}}



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

Reply via email to