我正在尝试使用 gradle kotlin DSL 作为 IntelliJ idea 中的构建系统来设置 kotlin 项目,但在尝试运行 buil.gradle.kts 文件时出现以下错误。我尝试过不同的 kotlin 编译器版本,但没有成功。
warning: default scripting plugin is disabled: The provided plugin org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar is not compatible with this version of compiler
error: unable to evaluate script, no scripting plugin loaded
Run Code Online (Sandbox Code Playgroud)
IntelliJ 版本:
摇篮版本:6.3
构建.gradle.kts
plugins {
id("org.jetbrains.kotlin.jvm") version "1.3.70"
// Apply the application plugin to add support for building a CLI application.
application
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository …Run Code Online (Sandbox Code Playgroud) 我是C ++的新手,我在下面的代码片段中遇到了这个代码片段,这对我来说似乎很奇怪。
const char* keys = "hello" "world";
std::cout << keys << std::endl;
Run Code Online (Sandbox Code Playgroud)
上面的代码在控制台中显示helloworld。在同一条语句中将两个字符串文字分配给const char *是否在语法上有效?如果是这样,它将如何存储在内存中?
我想以“秒”:“纳秒”格式获取 kotlin 中的 EPOCH 时间戳。
注意:请查看已接受的答案以获得正确的解决方案。
编辑:
这是我目前的解决方案,我确信会有更好的方法来实现这一目标,
import java.time.Instant
import java.time.temporal.ChronoUnit;
import kotlin.time.Duration.Companion.seconds
fun main() {
val epochNanoseconds = ChronoUnit.NANOS.between(Instant.EPOCH, Instant.now())
val epochSeconds = epochNanoseconds/1.seconds.inWholeNanoseconds
val remainingFractionOfNanoSeconds = epochNanoseconds%1.seconds.inWholeNanoseconds
println("$epochSeconds:$remainingFractionOfNanoSeconds")
}
Run Code Online (Sandbox Code Playgroud)
示例输出:
1670251213:849754000
Run Code Online (Sandbox Code Playgroud)
另一个例子(来自评论): 对于 1670251213 秒 50000 纳秒,也称为十进制 1670251213.00005 秒,我想要1670251213:50000(意味着:)。
有没有什么方法可以直接从java.time.Instant或任何其他可用的库中获取秒数和剩余纳秒数来方便地实现此目的?
已接受答案的解决方案:
import java.time.Instant
fun main() {
val time = Instant.now()
println("${time.epochSecond}:${time.nano}")
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读《Kotlin 协程深度探究》一书,以进一步了解协程。我在书中发现了以下陈述,我无法清楚地理解,如果有人用一个简单的例子来解释我将会有所帮助。
CancellationException可以使用 try-catch 捕获,但建议重新抛出它。
他还举了一个例子,
import kotlinx.coroutines.*
suspend fun main(): Unit = coroutineScope {
val job = Job()
launch(job) {
try {
repeat(1_000) { i ->
delay(200)
println("Printing $i")
}
} catch (e: CancellationException) {
println(e)
throw e
}
}
delay(1100)
job.cancelAndJoin()
println("Cancelled successfully")
delay(1000)
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,它抓住catch (e: CancellationException)并重新抛出它,现在我想知道如果我不重新抛出它会发生什么。我注释掉了,throw e但代码照常执行。
正如我从kotlin doc中看到的,CancellationException 用于结构化并发,这意味着 CancellationException 不会取消父级,而是通知父级取消父级的所有其他子级(我在这里吗?)
我相信我的理解是错误的,下面的代码证明了这一点,
import kotlinx.coroutines.*
fun main() = runBlocking {
val parentJob = launch …Run Code Online (Sandbox Code Playgroud) 我正在使用 libavfilter C API 来缩放我的视频。
我开始阅读与 libavfilter 相关的文档并陷入Filtergraph-description。
我不明白“过滤垫”这个术语。在整个文档中,我遇到了过滤器输入板和输出板等术语。
如果有人以简单的方式解释过滤垫,那将非常感激。另请分享与 libavfilter C API 示例相关的任何文档或链接。
下面的 MASM 代码的确切 NASM 等价物是什么?
; Simple lookup table (.const section data is read-only)
.const
const_array dword 0, 1, 1, 2, 3, 5, 8, 13, 21
Run Code Online (Sandbox Code Playgroud) 我正在使用箭头库在 Kotlin 中学习函数式编程,并且遇到了 Either 类型的折叠函数的奇怪行为(至少对我来说)。
import arrow.core.*
import arrow.syntax.function.pipe
object UserService {
fun findAge(user: String): Either<String, Option<Int>> {
return Either.Right(Some(1))
}
}
fun main(args: Array<String>) {
val anakinAge: Either<String, Option<Int>> = UserService.findAge("Anakin")
anakinAge.fold({itLeft -> itLeft.toUpperCase()},{itRight -> itRight.fold({ 1 }, {it})}) pipe ::println
}
Run Code Online (Sandbox Code Playgroud)
根据箭头折叠函数语法是
inline fun <C> fold(ifLeft: (A) -> C, ifRight: (B) -> C): C
Run Code Online (Sandbox Code Playgroud)
如果值为 Left,则执行ifLeft函数并返回类型C如果为 right,则执行 ifRight 并返回具有相同类型C的值,但在上面的代码片段中,如果值为 Left,则返回 String;如果值为 Right,则返回 Int 。它如何接受这种语法?
我遇到下面的泛型函数,它接受两种Either类型和一个函数作为参数。如果两个参数都Either.Right被应用到它上面并返回结果,如果任何一个参数是Either.Left它返回 NonEmptyList(Either.Left)。基本上它执行独立操作并累积错误。
fun <T, E, A, B> constructFromParts(a: Either<E, A>, b: Either<E, B>, fn: (Tuple2<A, B>) -> T): Either<Nel<E>, T> {
val va = Validated.fromEither(a).toValidatedNel()
val vb = Validated.fromEither(b).toValidatedNel()
return Validated.applicative<Nel<E>>(NonEmptyList.semigroup()).map(va, vb, fn).fix().toEither()
}
val error1:Either<String, Int> = "error 1".left()
val error2:Either<String, Int> = "error 2".left()
val valid:Either<Nel<String>, Int> = constructFromParts(
error1,
error2
){(a, b) -> a+b}
fun main() {
when(valid){
is Either.Right -> println(valid.b)
is Either.Left -> println(valid.a.all)
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码打印
[error 1, …Run Code Online (Sandbox Code Playgroud) 这将是一个基本问题,但我想不出解决方案。我需要从以下任一类型的右侧值中初始化一个常量。
val test: Either<String, Int> = 1.right()
Run Code Online (Sandbox Code Playgroud)
我尝试了类似下面的方法,但它缩小了常量的范围。
when(test) {
is Either.Right -> {val get:Int = test.b}
is Either.Left -> println(test.a)
}
Run Code Online (Sandbox Code Playgroud)
我希望get将其限定在when语句之外。有没有办法做到这一点,或者 Arrow 不是为此目的而制造的?
据我所知,字符串文字存储在只读内存中,并在运行时修改它导致分段错误,但我的下面的代码编译没有分段错误.
#include <string.h>
#include <stdio.h>
int main() {
char* scr = "hello";
strcpy(scr,scr);
printf("%s\n",scr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:你好
同样的事情,如果我试图将源字符串复制到不同的目标字符串文字,它会抛出一个分段错误
#include <string.h>
#include <stdio.h>
int main() {
char* scr = "hello";
char* dst = "hello";
strcpy(dst,scr);
printf("%s\n",dst);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:分段故障(核心转储)
根据K&R的书,strcpy()的实现类似于下面的内容
void strcpy(char *s, char *t)
{
while ((*s = *t) != '\0') {
s++;
t++;
}
}
Run Code Online (Sandbox Code Playgroud)
如果是这样,我应该在两种情况下都有分段错误.
编译细节:
gcc版本7.3.0(Ubuntu 7.3.0-27ubuntu1~18.04)