下面是Kotlin中的一个简单的泛型函数: fun <T> twice(x: T) : T { return 2 * x } 尝试构建它(在项目或REPL中)会导致以下错误: error: none of the following functions can be called with the arguments supplied:
public final operator fun times(other: Byte): Int defined in kotlin.Int
public final operator fun times(other: Double): Do
我正在探索Apache模板,这些模板由GoogleCloudPlatform在上提供。
特别是,我正在将模板从Java转换为Kotlin。
通过这样做,我在在线MapElements.input(...).via(...)变换中得到了一个过载模糊度解决错误。错误信息是:
Error:(62, 22) Kotlin: Cannot choose among the following candidates without completing type inference:
public final fun <NewInputT : Any!> via(fn: ((input: B
在以下代码中:
val sum = listOf(1, 2, 3).sumOf { if (it % 2 == 0) 1 else 0 }
Kotlin给出了以下错误:
Kotlin: Overload resolution ambiguity:
public inline fun <T> Iterable<TypeVariable(T)>.sumOf(selector: (TypeVariable(T)) -> Int): Int defined in kotlin.collections
public inline fun <T> Iterabl
简单地说,在Kotlin (如kotlin.io.println )中,一个引用/迭代如何在重载的顶层函数上进行反射。
鉴于以下情况:
object Bar {
fun foo(x: Int) = Unit
fun foo(x: Byte) = Unit
fun foo(x: Float) = Unit
}
通过执行以下操作,我可以遍历foo的各种重载:
fun main() {
Bar::class.memberFunctions
.filter { kFunction -> kFunction.name == "foo"
我有一堂课:
open data class Person(var name: String)
另一班:
data class Student(var reg: String) : Person("")
这给了我一个错误:
错误:修饰符“open”与“data”不兼容
如果我从Person类中删除数据,就可以了。
为什么kotlin打开和数据不兼容?
val me = "Hello I am learning Kotlin"
fun main(){
println(me[1,10])
}
在第3行中,我得到了“公开的、公开的、有趣的get(index: Int):kotlin.String中定义的Char”错误,为什么呢?我怎样才能在科特林打印出一根线的一部分。根据,第3行应该可以工作,但是它给了我太多的参数错误。
考虑下面的守则:
struct Bar{};
struct Foo
{
Foo() = default;
Foo(const Bar&) {}
Foo(const Foo&) = delete;
// IMPLICIT conversion to Bar
operator Bar(){return {};}
};
int main()
{
Foo f1;
Foo f2(static_cast<Bar>(f1)); // this is OK
Foo f3(f1); // does not com
我有以下构造函数:
open class IPFS @JvmOverloads constructor(protected val base_url: String = "http://127.0.0.1:5001/api/v0/",
protected val okHttpClient: OkHttpClient = OkHttpClient.Builder().build(),
protected val m
我是Kotlin的初学者。我非常熟悉Python,在进入Kotlin之前,我只阅读了基本的教程。
在阅读科特林的这一节文献时,我脑海中浮现了一个问题。
我从文件的上面一节中了解到:
对于赋值=,会发生隐式类型转换。如果左侧的类型是超类型或相同类型的右侧类型,则代码将编译。否则就是编译错误。对于Int和Long,它们都是Number的子类型,但它们都不是彼此的子类型,因此隐式转换无法工作。因此,我们需要使用像.toLong()或.toInt()这样的方法来显式地转换它们。
然后当我读到这部分
val l= 1L +3 // Long +国际=> Long
我开始怀疑在
在Kotlin中,我可以重写一些操作符,例如+ by plus(),* by times() .但是对于像集合这样的东西,首选的(集合论)符号/运算符并不存在。例如,A∩B用于交叉口,A∪B用于union。
我似乎不能定义我自己的运算符,没有明确的语法来说明要为运算符使用什么符号。例如,如果我想作为操作符为$$创建一个函数:
operator fun String.$$(other: String) = "$this !!whatever!! $other"
// or even
operator fun String.whatever(other: String) =
我的kotlin密码如下:
return Failsafe.with<Boolean>(retryPolicy)
// Here I want fall back to false
.withFallback { -> false} // Does not work
.get { ->
publish(event)
true
}
Kotlin编译器抱怨道:
错误:(89,14) Kotlin:过载解决歧义:
公共最终乐趣withFallback(p0:(()
此代码段无法在、和中编译(它们提供不同的错误消息):
int foo(int a, int b) {
return a + b;
}
template <class Ret, class A, class B>
void foo(Ret (*)(A, B)) {
}
int main() {
foo(foo);
return 0;
}
这不是应该编译吗?我不明白为什么它不能解析重载的函数或推断模板参数。任何帮助都欢迎,谢谢。
PS:如果用void foo(int (*)(int, int))替换模板,或者重命名一个foo以避免重载,它就会编译。
我正在尝试将这段java代码转换为kotlin。
public int compare1(Comparable c, Object o) {
return c.compareTo(o);
}
到kotlin代码:
fun compare1(c: Comparable<*>, o: Any?): Int {
return c.compareTo(o)
}
但要犯错误
Type mismatch: inferred type is Any? but Nothing was expected
为什么会发生这个错误?谢谢
考虑一下这个Kotlin类的类注释:
/**
* This class has two methods, one that takes one parameters ([foo]),
* and another one that takes two parameters ([foo]).
**/
class Clazz {
/* Foo with one. */
fun foo(a: Int) { }
/* Foo with two. */
fun foo(a: Int, b: Int) { }
}
我希望第二个链接指向第二个函数(一个有两个参数)。