문제

I was interested in testing performance gain for groovy++ over plain groovy. I found the script to test

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"
}

It works. But if I try to add

@Typed

before first class name and run I'm getting error:

#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 Version: 1.7.5 JVM: 1.6.0_18

Any ideas why?

도움이 되었습니까?

해결책

To enjoy statically typed compilation you need to provide at least some amount of type information.

Normally it is enough to define types of properties (next, prev in your case) and types of method parameters.

다른 팁

All the variables you declare are of type java.lang.Object (Or grovy.lang.Object in this case). So they don't have the methods "next" etc.

Try to use Person current = new Person() and Cain current = first etc.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top