小编ers*_*tan的帖子

为什么'kotlin.Result'不能用作返回类型

我已经创建了一个方法,并且返回属于Result<R>一个类MyClass<R>,但错误信息是'kotlin.Result'不能用作返回类型

我还查看了一些提示的Result源代码; 为什么会这样?

测试代码(使用v.1.3-RC):https://try.kotlinlang.org/#/UserProjects/ueeogpr0cqnovot7o4ooa42dv5/l9qidpqj9pf1i7rablka4u5qjt

class MyClass<R>(val r:R){
    fun f():Result<R>{ // error here
        return Result.success(r)
    }
}

fun main(args: Array<String>) {
    val s = Result.success(1)
    val m = MyClass(s)   
}
Run Code Online (Sandbox Code Playgroud)

function return-type kotlin

47
推荐指数
4
解决办法
7924
查看次数

在 Kotlin 中访问函数的默认参数值

可以从函数扩展或其他任何地方访问函数的默认参数值吗?

fun DieRoll.cheatRoll():Int = roll(min = max -1)

fun roll(min: Int = 1, max: Int = 6): Int = (min..max).rand()
Run Code Online (Sandbox Code Playgroud)

default-value default-parameters kotlin

5
推荐指数
1
解决办法
3392
查看次数

扩展类条件分支超值的 Kotlin 二级构造函数

您如何根据第四个二级构造函数中的值进行条件超级调用?为什么这不起作用?

open class SecondaryConstructors{
    constructor(i:Int)
    constructor(s:String)
}
class SecondaryExtended:SecondaryConstructors {
    constructor(i:Int):super(i)
    constructor(s:String):super(s)
    constructor():super(if(true)1 else 0)
    constructor(intOrString:Boolean):super( if(intOrString) 3 else "hey")
    // conditional branch result of int/string is implicitly cast to Any
    // error - none of the following functions can be called with the arguments supplied
}
Run Code Online (Sandbox Code Playgroud)

constructor kotlin

3
推荐指数
1
解决办法
865
查看次数

Kotlin委托属性由lazy本地线程

是否有一种简单的方法可以获得by lazy每个线程计算的委托属性值ThreadLocal

LazyThreadSafetyMode控制并发初始化,.NONE通过允许多个线程接收不同的值来接近所需的功能,但后续的后初始化调用引用相同的对象,返回相同的奇异值而不管线程,在某些情况下返回null.

无论是并发初始化还是后期初始化,该属性都会为每个线程缓存一个唯一值.

delegates properties lazy-evaluation thread-local kotlin

3
推荐指数
1
解决办法
460
查看次数