Koltin
我已经创建了一个事物列表( list of things ),并且希望跳过那些不包含字符'o'的东西。我使用的是str.contains()函数,但这是抛出的错误。
fun main(args: Array<String>) {
val list = listOf<Any>("book", "table", "laptop", "pen")
for (str in list)
{
if (**!str.contains('
请看下面的代码列表:
package com.test;
class XParam implements Comparable<XParam>{
@Override
public int compareTo(XParam o) {
// TODO Auto-generated method stub
return 0;
}
}
public class Main {
public static void main(String[] args) {
Comparable<XParam>
为什么它不编译?它告诉我们list.sortBy中有一些错误
fun <T, R : Comparable<R>> Iterable<T>.sortBy(vararg selectors: (T) -> R): List<T> {
return this.sortedWith(compareBy(*selectors))
}
fun main() {
class Person(val name: String, val age: Int)
val list = listOf(Person("Alex", 20)
我正在尝试将这段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的min()/max()别名实现Comparable<T>coerceAtLeast()/coerceAtMost(),如果没有其他的扩展接口的话(我到目前为止只扩展了类)。
我试过这个:
fun <T>Comparable<T>.max(other:T) : T {
return this.coerceAtLeast(other)
}
但我得到了以下错误:
Type inference failed: Cannot infer type parameter T in fun <T : Comparable<T#1 (t
正如标题所说,类型是正确推断的(见下图),但我仍然收到一个TS错误,抱怨any类型。完整的代码在图片下方。 这是一个编辑器问题还是TypeScript问题?也许还有其他我没有考虑过的事情?谢谢。 ? export function createArrayCycle<T>(array: T[]): <U>(func?: (arg: T) => U) => T {
let cycleCount = 0;
return function cycle<U>(func): T | U {
if (cycleCount === a
我有以下函数,它应该列出AWS S3桶中的所有顶级文件夹。它使用的是,它本身是用打字本编写的。
async function listTopLevelFolders() {
let ContinuationToken: string | undefined = undefined
do {
// type inference does not work, command is of type any
const command = new ListObjectsV2Command({
Bucket,
ContinuationToke
在下面的第一个代码片段中,case语句在函数中定义,并按预期工作。
代码片段1
def echoWhatYouGaveMe(x: Any): String = x match {
case (a, b) => s"got $a and $b"
case (a, b, c) => s"got $a, $b, and $c"
case _ => "Unknown"
}
object MatchTest extends App {
// trigger the tuple patterns
这小块scala代码在编译时会产生一个奇怪的错误。
import scala.collection.mutable.ListBuffer
object main {
def main(args: Array[String]) {
val rs = new ListBuffer[String]()
val ns = scala.collection.mutable.Map[String, String]()
"A very long string".split("\\ ") foreach { word =>
在我的角度项目中,我收到了以下错误:Type '{}' is missing the following properties from type 'any[]'
我有一个简单的角代码,其中有一个助手类向url发出请求,它向url发送像[ {a: b, c: d}, {e: f} ... ]这样的JSON数组,所有东西都正常工作,具有预期的结果,但是当我试图将结果返回到主页时。
我会继续强制这个类型,但是不知道我可能需要什么来解决这个错误并想要理解它。
我很乐意回答关于密码的问题。
HelpClass
export class ExampleJsonGette
我以为我理解泛型。但是,在本例中,我无法理解为什么要编译行isIn1(new Class2(1), arr);,而不能编译行isIn2(new Class2(1), arr);。
class E1{};
class E2{};
class Class1 implements Comparable<Class1> {
int i;
public Class1(int i) {
this.i = i;
}
@Override
public boolean equals(Object o) {
if (this
下面是Java中泛型类的一段代码
public class PairTest{
public static void main(String[] args){
LocalDate[] birthdays = {....} // initialize birthdays array
Pair<LocalDate> mm = ArrayAlg.minmax(birthdays); // THIS LINE !
}
}
public ArrayAlg{
public static < T extends Compara
首先,我试过:
interface Super
class A : Super
class B : Super
val a = if (System.currentTimeMillis() >= 100) A() else B()
我按了Ctrl Q以检查a的类型。这是Super,就像预期的那样。
但当我试着:
val someVar = if (System.currentTimeMillis() > 0) 1 else 1.0
它说someVar是Any。不是应该是Number吗?
class A<T>
{
class InnerA<U>
{}
}
public class Main
{
public static void main(String[] args)
{
A<Integer>.InnerA<String> var = new A<>().new InnerA<>();
}
}
当编译上面的代码片段时,它会导致编译时出现错误,上面写着“不兼容类型:无法推断A.InnerA<>的类型参数。在我看来,编译器应该能够推断类型为
在浏览String的docjar时,我碰巧看到了以下代码:
public static final Comparator<String> CASE_INSENSITIVE_ORDER
= new CaseInsensitiveComparator();
private static class CaseInsensitiveComparator
implements Comparator<String>, java.io.Serializab
有谁能解释一下为什么下面的代码不起作用:
public static void main(String[] args) throws IOException
{
Comparable<Integer> q = new Integer(4);
Comparable<Integer> o = new Integer(4);
// Problematic line
int j = o.compareTo(q);
if (j == -1)
System.out.println("yes");
e
下面的代码中有一个错误,它包含了大量的文本。当我将标有箭头的行中的泛型的Y参数更改为数字或字符串等类型时,编译后没有错误
问题1-这是编译器的错误吗?问题2-如何修复错误?
class channel<S,Y>{
public merge(...channels:channel<any, Y>[]) {
var ch = new channel<void, channel<any, Y>>(); // <---
var result = ch.flatMap(x => x);
return result
我有以下方法来规范化Java M3节点: public node normalizeNodeDec(ast) {
return top-down visit (ast) {
case \method(x, _, y, z, q) => \method(Type::short(), "methodName", y, z, q)
case \method(x, _, y, z) => \method(Type::short(), "methodName", y, z)
case \parameter(x, _, z) =>
下面是我遇到的错误代码:- fun main(){ val nullList = listOf(1, 3, null, "chair", "table", null)
val nullArrayList = arrayListOf("this", "house", null, 3, 9)
println("This list without the null values is ${nullList.filterNotNull()}")
println("This array without t