【问题标题】:Groovy++ error when adding @Typed添加 @Typed 时出现 Groovy++ 错误
【发布时间】:2011-01-02 11:11:05
【问题描述】:

我有兴趣测试 groovy++ 相对于普通 groovy 的性能提升。 我找到了要测试的脚本

class Chain
{
    def size
    def first

    def init(siz)
    {
        def last
        size = siz
        for(def i = 0 ; i < siz ; i++)
        {
            def current = new Person()
            current.count = i
            if (i == 0) first = current
            if (last != null)
            {
                last.next = current
            }
            current.prev = last
            last = current
        }
        first.prev = last
        last.next = first
    }

    def kill(nth)
    {
        def current = first
        def shout = 1
        while(current.next != current)
        {
            shout = current.shout(shout,nth)
            current = current.next
        }
        first = current
    }
}

class Person
{
    def count
    def prev
    def next

    def shout(shout,deadif)
    {
        if (shout < deadif)
        {
            return (shout + 1)
        }
        prev.next = next

        next.prev = prev
        return 1
    }
}

def main(args)
{
    println "Starting"
    def ITER = 100000
    def start = System.nanoTime()
    for(def i = 0 ; i < ITER ; i++)
    {
        def chain = new Chain()
        chain.init(40)
        chain.kill(3)
    }
    def end = System.nanoTime()
    println "Total time = " + ((end - start)/(ITER * 1000)) + " microseconds"
}

它有效。但是如果我尝试添加

@Typed

在第一个类名和运行之前出现错误:

#groovy groovy.groovy

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/melco/test/groovy.groovy: 18: Cannot find property next of class Object
 @ line 18, column 22.
                   last.next = current
                        ^

1 error

# groovy -version

Groovy 版本:1.7.5 JVM:1.6.0_18

有什么想法吗?

【问题讨论】:

    标签: groovy groovy++


    【解决方案1】:

    要享受静态类型编译,您至少需要提供一些类型信息。

    通常定义属性类型(下一个,上一个在你的情况下)和方法参数的类型就足够了。

    【讨论】:

    【解决方案2】:

    您声明的所有变量都是 java.lang.Object 类型(在本例中为 grovy.lang.Object)。所以他们没有“下一步”等方法。

    尝试使用Person current = new Person()Cain current = first等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      相关资源
      最近更新 更多