“返回此”的类型在 Groovy @Mixin 中

发布于 2024-12-01 19:49:13 字数 794 浏览 1 评论 0原文

我有一个 mixin 类,它捆绑了不具有共同遗产的不同类型的功能。混合是使用@Mixin注释应用的,因此它是在编译时处理的。

一些 mixin 方法返回 this 作为方法调用的结果。问题是 this 是混合类型,而不是基类的类型。当我想在应用程序的其余部分中键入时,会抛出 ClassCastException ,表示混合类型无法转换为基本类型。

在下面的示例代码中,return this 返回 AMixin 类型的对象,而不是 BaseClass 类型的对象。

如何让 return this 返回 BaseClass 类型的对象而不是 AMixin 类型的对象?

class AMixin {

  def getWhatIWant(){
    if(isWhatIwant){
      return this
    } else {
      getChildWhatIWant()
    }
  }

  def getChildWhatIWant(){
    for (def child in childred) {
        def whatIWant = child.getWhatIWant()
        if (whatIWant) {
            return whatIWant
        }
    }
    return null
  }
}


@Mixin(AMixin)
class BaseClass {
  boolean isWhatiWant
  List<baseClass> children
}

I have a mixin class that bundles functionality for different types that do not share a common heritage. The mixing is applied using the @Mixin annotation, so it is handled at compile time.

Some of the mixin methods return this as the result of a method call. The problem is that the this is of the mixing type and not the type of the base class. When I want to work typed in the rest of the application a ClassCastException is thrown saying that the mixing type can not be cast to the base type.

In the example code below return this returns an object of type AMixin instead of an Object of type BaseClass.

How can I have return this return an object of type BaseClass instead of an object of type AMixin?

class AMixin {

  def getWhatIWant(){
    if(isWhatIwant){
      return this
    } else {
      getChildWhatIWant()
    }
  }

  def getChildWhatIWant(){
    for (def child in childred) {
        def whatIWant = child.getWhatIWant()
        if (whatIWant) {
            return whatIWant
        }
    }
    return null
  }
}


@Mixin(AMixin)
class BaseClass {
  boolean isWhatiWant
  List<baseClass> children
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永言不败 2024-12-08 19:49:13

我刚刚遇到了同样的情况。我通过将具体类中的“this”设置为具体类中的私有变量“me”并在 Mixin 类中返回“me”来解决这个问题。例如:

class MyMixin {
    def mixinMethod() {
        // do stuff
        return me
    }
}

@Mixin(MyMixin)
class MyConcreteClass {
    private MyConcreteClass me
    MyConcreteClass() {
        me = this
    }
}

我觉得这有点笨拙,但我认为它比其他解决方案简单得多。我个人需要能够在多个类中使用相同的 Mixin,如果您无法将多个类别分配给单个 Mixin 类,那么听起来其他建议的解决方案将不允许这样做。

I just ran into this same situation. I solved it by setting 'this' from the concrete class into a private variable 'me' inside the concrete class and return 'me' in the Mixin classes. For example:

class MyMixin {
    def mixinMethod() {
        // do stuff
        return me
    }
}

@Mixin(MyMixin)
class MyConcreteClass {
    private MyConcreteClass me
    MyConcreteClass() {
        me = this
    }
}

I feel like it's a bit kludgy, but I think it's a lot simpler than this other solution. I personally need the ability to use the same Mixin in multiple classes, and it sounds like this other proposed solution would not allow for that if you cannot assign multiple Categories to a single Mixin class.

尘世孤行 2024-12-08 19:49:13

我创建了 Base 类,并将类别添加到 AMixin 类中,并且 BaseClass 从 Base 扩展而来......(http://groovy.codehaus.org/Category+and+Mixin+transformations

在 GroovyConsole 中执行此操作我得到

BaseClass@39c931fb

class Base {
     boolean isWhatIwant
     List<BaseClass> children
}

@Category(Base)
class AMixin {

  def getWhatIWant(){
    if(isWhatIwant){
      return this
    } else {
      getChildWhatIWant()
    }
  }

  def getChildWhatIWant(){
    for (def child in children) {
        def whatIWant = child.getWhatIWant()
        if (whatIWant) {
            return whatIWant
        }
    }
    return null
  }
}


@Mixin(AMixin)
public class BaseClass extends Base {

}

def b = new BaseClass(isWhatIwant:true)
println b.getWhatIWant()

编辑只是虚拟类。我知道它的工作原理非常尴尬......我确信 Guillaume Laforge 可以回答这个问题作品...

class DummyClass {

}

@Category(DummyClass)
class AMixin {

  def getWhatIWant(){
    if(isWhatIwant){
      return this
    } else {
      getChildWhatIWant()
    }
  }

  def getChildWhatIWant(){
    for (def child in children) {
        def whatIWant = child.getWhatIWant()
        if (whatIWant) {
            return whatIWant
        }
    }
    return null
  }
}


@Mixin(AMixin)
public class BaseClass extends DummyClass {
     boolean isWhatIwant
     List<BaseClass> children
}

def b = new BaseClass(isWhatIwant:true)
println b.getWhatIWant()

I created the class Base added the category to the AMixin Class and the BaseClass extends from Base.....(http://groovy.codehaus.org/Category+and+Mixin+transformations)

Executed this in GroovyConsole I get

BaseClass@39c931fb

class Base {
     boolean isWhatIwant
     List<BaseClass> children
}

@Category(Base)
class AMixin {

  def getWhatIWant(){
    if(isWhatIwant){
      return this
    } else {
      getChildWhatIWant()
    }
  }

  def getChildWhatIWant(){
    for (def child in children) {
        def whatIWant = child.getWhatIWant()
        if (whatIWant) {
            return whatIWant
        }
    }
    return null
  }
}


@Mixin(AMixin)
public class BaseClass extends Base {

}

def b = new BaseClass(isWhatIwant:true)
println b.getWhatIWant()

EDIT just a DummyClass. I know it's very awkward that It works....I'm sure Guillaume Laforge could answer how this works...

class DummyClass {

}

@Category(DummyClass)
class AMixin {

  def getWhatIWant(){
    if(isWhatIwant){
      return this
    } else {
      getChildWhatIWant()
    }
  }

  def getChildWhatIWant(){
    for (def child in children) {
        def whatIWant = child.getWhatIWant()
        if (whatIWant) {
            return whatIWant
        }
    }
    return null
  }
}


@Mixin(AMixin)
public class BaseClass extends DummyClass {
     boolean isWhatIwant
     List<BaseClass> children
}

def b = new BaseClass(isWhatIwant:true)
println b.getWhatIWant()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文