简体   繁体   English

我只是无法理解 Groovy 类型的 Java 心态

[英]I just can't understand Groovy types with Java mindset

I am struggling to comprehend why code which in Java works, then fails in groovy. For example:我很难理解为什么 Java 中的代码有效,然后在 groovy 中失败。例如:

I've found that if I use Tuple2.of(... I will get a compilation error, but new Tuple2(.. works. Why?我发现如果我使用Tuple2.of(...我会得到一个编译错误,但是new Tuple2(..有效。为什么?

    static Stream<Tuple2<LocalDate, Double>> forecastEachMonth(Tuple2<LocalDate, Double> openingBalance, Double rate){

        Stream<Tuple2<LocalDate, Double>> stream = Stream.iterate(
                openingBalance,
                {

                    LocalDate current = it.first
                    LocalDate nextDate = it.first.plusMonths(1)

                    int days = Days.daysBetween(current, nextDate).days
                    double years = days / 365.0
                    double interest = it.second * rate * years
                    double nextBalance = it.second + interest

                    // return Tuple2.of(nextDate, nextBalance) // exception after 2 iterations, Have no idea why.
                    return new Tuple2(nextDate, nextBalance)
                }
        )
        return stream
    }

You can test this out:你可以测试一下:

        Stream<Tuple2<LocalDate,Double>> test = forecastEachMonth(
                        LocalDate.now(),
                        200000.0d,
                0.05d
        )
        println test.limit(200).collect(Collectors.toList())

Gives an error:报错:

Expected earlier checking to detect generics parameter arity mismatch
Expected: groovy.lang.Tuple<E> 
Supplied: groovy.lang.Tuple<org.joda.time.LocalDate,java.lang.Double> 

But Intellij knows the correct type when I hover over the local variable.但是当我在局部变量上输入 hover 时,Intellij 知道正确的类型。

I've also converted the project to Java and it just works.我还将项目转换为 Java 并且它可以正常工作。 Which severely plagues my experience coding in Groovy, as I lose heaps of time on code which works in Java but not in Groovy. (Albeit there is no Tuple in Java I just used another library having the collection Pair).这严重困扰了我在 Groovy 中编码的经验,因为我在 Java 中有效但在 Groovy 中无效的代码上浪费了大量时间。(尽管 Java 中没有元组,但我只是使用了另一个具有 Pair 集合的库)。 But I just can't get my head around why Groovy suddenly doesn't know the type of a given variable, reverts to Object, then just throws an exception, how is that desirable?但我无法理解为什么 Groovy 突然不知道给定变量的类型,恢复为 Object,然后抛出异常,这有什么可取的?

can't understand your "java" pain.无法理解你的“java”痛苦。

the following code sample works well even with compile static:以下代码示例即使在编译 static 时也能正常工作:

@groovy.transform.CompileStatic
def f(){
  Tuple2<Date, Double> a = new Tuple2(new Date(), 1)
  Tuple2<Date, Double> b = new Tuple2(new Date(), 2)

  List<Tuple2<Date, Double>> list = [a,b]
  List<Tuple2<Date, Double>> result = list.takeWhile{Tuple2<Date, Double> e-> e.getV2()<2}

  assert result instanceof List
  assert result.size()==1
  assert result[0].getV2()<2
  assert result[0] instanceof Tuple2
}

f()

your questions:你的问题:

I've found that if I use Tuple2.of(... I will get a compilation error, but new Tuple2(.. works. Why?我发现如果我使用Tuple2.of(...我会得到一个编译错误,但是new Tuple2(..有效。为什么?

there is not such method Tuple2.of(... in Tuple2 class没有这样的方法Tuple2.of(... in Tuple2 class

why Groovy suddenly doesn't know the type of a given variable, reverts to Object, then just throws an exception, how is that desirable?为什么 Groovy 突然不知道给定变量的类型,恢复为 Object,然后抛出异常,这有什么可取的?

i used your code with minimal changes to make it runnable and it works without any error:我使用您的代码进行了最少的更改以使其可运行,并且可以正常运行:

import java.time.*
import java.util.stream.*

static Stream<Tuple2<LocalDate, Double>> forecastEachMonth(Tuple2<LocalDate, Double> openingBalance, Double rate){

    Stream<Tuple2<LocalDate, Double>> stream = Stream.iterate(
            openingBalance,
            {

                LocalDate current = it.first
                LocalDate nextDate = it.first.plusMonths(1)

                int days = nextDate - current
                double years = days / 365.0
                double interest = it.second * rate * years
                double nextBalance = it.second + interest

                return new Tuple2(nextDate, nextBalance)
            }
    )
    return stream
}


Stream<Tuple2<LocalDate,Double>> test = forecastEachMonth(
    new Tuple2(LocalDate.now(),200000.0d),
    0.05d
)
println test.limit(200).collect(Collectors.toList())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM