0

根据这部分文档,Reactor 应该支持 null-safety: https ://projectreactor.io/docs/core/release/reference/#kotlin-null-safety

尽管如此,使用map我可以轻松地跳过空值检查:

internal class NullabilityTest {
    @Test
    fun nullability() {
        val userWithMap: Mono<String> = UserRepo.findUser(5)
            .map { it?.getName() }//this returns String?, it should not compile 
    }
}

class User {
    fun getName() = "Mike"
}

class UserRepo {
    companion object {
        fun findUser(id: Int): Mono<User?> {
            return Mono.empty()
        }
    }
}

如本例所示,String?分配给Mono<String>. 在这种情况下收到编译器故障会很棒。它是反应堆中的错误还是从未实施过的东西?

我正在使用:反应堆 3.2.12

4

1 回答 1

0

问题是它map上面没有注解,而且由于它是用 Java 编写的,R是代码中的平台类型,所以 Kotlin 编译器不会抱怨:

    /**
     * Transform the item emitted by this {@link Mono} by applying a synchronous function to it.
     *
     * <p>
     * <img class="marble" src="doc-files/marbles/mapForMono.svg" alt="">
     *
     * @param mapper the synchronous transforming {@link Function}
     * @param <R> the transformed type
     *
     * @return a new {@link Mono}
     */
    public final <R> Mono<R> map(Function<? super T, ? extends R> mapper) {
        if (this instanceof Fuseable) {
            return onAssembly(new MonoMapFuseable<>(this, mapper));
        }
        return onAssembly(new MonoMap<>(this, mapper));
    }
于 2019-09-18T10:55:40.150 回答