美文网首页
Kotlin踩坑记

Kotlin踩坑记

作者: hohov | 来源:发表于2018-06-23 10:50 被阅读0次

需求:

CompletableFuture的suppayAsync()方法

public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)

kotlin 写法
val executor = Executors.NewExecutors.newFixedThreadPool(Math.min(shops.size, 100)) { r ->
    val t = Thread(r)
    t.isDaemon = true
    t
}
val priceFutures = shops.stream()
        .map { shop ->
            CompletableFuture.supplyAsync(
                    { "${shop.name} price is ${shop.getPrice(shop.name)}" }, executor)
        }.collect(Collectors.toList())

没错吧, kotlin的lambda就是这么写的啊, 可是报错


image01.png

喷了, 单参数的supplyAsync这样写没有问题的啊,


image02.png
想到了IDEA可以将java代码转换成kotlin代码, 妈蛋那我写java版本的转换一下,
java版本
image03.png

转换


image04.png
image05.png

又喷了, 这和我之前自己写的一模一样啊, 依然报错啊. 放大招, 我强转


image06.png
不报错了.运行一下, 结果:
image07.png
强转也不行啊, 这可咋整.
没办法, 只有写匿名内部类了
image08.png

可以了, 也可以运行, 但是, 哎哟我擦, 那条黄线什么意思!


image09.png
再次喷了, 提示我转lambda, 不会还是和之前转的一样吧, 试试
image10.png
哎哟我擦. 要主动构造一个Supplier<String>才可以啊

相关文章

网友评论

      本文标题:Kotlin踩坑记

      本文链接:https://www.haomeiwen.com/subject/sqjryftx.html