Reuse function with return type always equal to the struct that is calling that function in golang

huangapple 未分类评论87阅读模式
英文:

Reuse function with return type always equal to the struct that is calling that function in golang

问题

我正在尝试在Golang中做一些在Java中使用Type可以轻松完成的事情。例如:

  1. public class MyClass<Type extends MyClass<Type>> {
  2. public Type myMethod() {
  3. // Do something
  4. return (Type) this;
  5. }
  6. }
  7. public class Test1 extends MyClass<Test1> {
  8. public String A;
  9. Test1(String A) {
  10. this.A = A;
  11. }
  12. }
  13. public class Test2 extends MyClass<Test2> {
  14. public String B;
  15. Test2(String B) {
  16. this.B = B;
  17. }
  18. }

因此,在主函数中,我可以这样做:

  1. Test1 t1 = new Test1("hello");
  2. Test2 t2 = new Test2("world");
  3. System.out.println(t1.myMethod().A); // 输出 "hello"
  4. System.out.println(t2.myMethod().B); // 输出 "world"

我正在尝试弄清楚在Go中是否有一种类似的方法。也许可以这样做:

  1. type test1 struct {
  2. A string
  3. }
  4. type test2 struct {
  5. B string
  6. }
  7. func (val Type) MyMethod() Type {
  8. // Do something
  9. return val
  10. }
  11. func main() {
  12. t1 := test1{A: "hello"}
  13. t2 := test2{B: "world"}
  14. fmt.Println(t1.MyMethod().A) // 输出 "hello"
  15. fmt.Println(t2.MyMethod().B) // 输出 "world"
  16. }
英文:

I'm trying to do in Golang something that would be easily done in Java with Type. For example:

  1. public class MyClass&lt;Type extends MyClass&lt;Type&gt;&gt; {
  2. public Type myMethod() {
  3. // Do something
  4. return (Type) this;
  5. }
  6. }
  7. public class Test1 extends MyClass&lt;Test1&gt; {
  8. public String A;
  9. Test1(String A) {
  10. this.A = A;
  11. }
  12. }
  13. public class Test2 extends MyClass&lt;Test2&gt; {
  14. public String B;
  15. Test2(String B) {
  16. this.B = B;
  17. }
  18. }

So, in main I can do

  1. Test1 t1 = new Test1(&quot;hello&quot;);
  2. Test2 t2 = new Test2(&quot;world&quot;);
  3. System.out.println(t1.myMethod().A); // Prints &quot;hello&quot;
  4. System.out.println(t2.myMethod().B); // Prints &quot;world&quot;

I'm trying to figure out if there is a way to do somethig similar in Go.
Maybe something like:

  1. type test1 struct {
  2. A string
  3. }
  4. type test2 struct {
  5. B string
  6. }
  7. func (val Type) MyMethod() Type {
  8. // Do something
  9. return val
  10. }
  11. func main() {
  12. t1 := test1{A: &quot;hello&quot;}
  13. t2 := test2{B: &quot;world&quot;}
  14. fmt.Println(t1.MyMethod().A) // Prints &quot;hello&quot;
  15. fmt.Println(t2.MyMethod().B) // Prints &quot;world&quot;
  16. }

答案1

得分: 0

感谢JimB,我找到了一种方法。

从Go 1.18开始,支持泛型类型。

因此,在Go中,Java的MyClass将类似于以下代码:

  1. type MyGeneric[T any] struct {
  2. self *T
  3. }
  4. func (val MyGeneric[T]) MyMethod() *T {
  5. // 做一些操作
  6. return val.self
  7. }

Java类Test1将是:

  1. type test1 struct {
  2. MyGeneric[test1]
  3. A string
  4. }
  5. func MakeTest1(A string) test1 {
  6. answer := test1{A: A}
  7. answer.self = &answer
  8. return answer
  9. }

类似地,Java类Test2将是:

  1. type test2 struct {
  2. MyGeneric[test2]
  3. B string
  4. }
  5. func MakeTest2(B string) test2 {
  6. answer := test2{B: B}
  7. answer.self = &answer
  8. return answer
  9. }

所以,主函数如我们所想:

  1. t1 := MakeTest1("hello")
  2. t2 := MakeTest2("world")
  3. fmt.Println(t1.MyMethod().A) // 输出 "hello"
  4. fmt.Println(t2.MyMethod().B) // 输出 "world"
英文:

Thanks to JimB I found a way.

From Go 1.18 Generic Types are sypported.

So the Java MyClass will look something like this in Go:

  1. type MyGeneric[T any] struct {
  2. self *T
  3. }
  4. func (val MyGeneric[T]) MyMethod() *T {
  5. // Do something
  6. return val.self
  7. }

The Java class Test1 will be:

  1. type test1 struct {
  2. MyGeneric[test1]
  3. A string
  4. }
  5. func MakeTest1(A string) test1 {
  6. answer := test1{A: A}
  7. answer.self = &amp;answer
  8. return answer
  9. }

And similarly the Java class Test2:

  1. type test2 struct {
  2. MyGeneric[test2]
  3. B string
  4. }
  5. func MakeTest2(B string) test2 {
  6. answer := test2{B: B}
  7. answer.self = &amp;answer
  8. return answer
  9. }

So the main is as we wanted

  1. t1 := MakeTest1(&quot;hello&quot;)
  2. t2 := MakeTest2(&quot;world&quot;)
  3. fmt.Println(t1.MyMethod().A) // Prints &quot;hello&quot;
  4. fmt.Println(t2.MyMethod().B) // Prints &quot;world&quot;

huangapple
  • 本文由 发表于 2022年7月18日 22:03:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/73023641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定