我试图通过尝试使用Scalatra来进入Scala.我不久前在课程上完成了Martin Odersky的课程,但我仍然很难理解这是如何工作的:
package com.example.app
import org.scalatra._
class HelloWorldApp extends ScalatraFilter {
get("/") {
<h1>Hello, {params("name")}</h1>
}
}
Run Code Online (Sandbox Code Playgroud)
此示例取自Scalatra主页:http://www.scalatra.org/
如何执行此声明:
get("/") {
Hello, {params("name")}
}
是{}参数的get()方法吗?你能解释一下Scala语言的哪些特性在使用吗?Scalatra源代码的指针将是完美的定义,这将是完美的.
我正在考虑在 Ubuntu 服务器上运行基于 libGDX 的应用程序。服务器没有 GPU。目的是渲染定义为 libGDX“游戏”的动画。完美的设置会将帧通过管道输出到标准输出。我对某种软件模拟 GPU 很满意。
我知道 libGDX 无头模式,但据我了解它不会渲染任何图形。
此代码基于Coroutines指南示例:扇出
val inputProducer = produce<String>(CommonPool) {
(0..inputArray.size).forEach {
send(inputArray[it])
}
}
val resultChannel = Channel<Result>(10)
repeat(threadCount) {
launch(CommonPool) {
inputProducer.consumeEach {
resultChannel.send(getResultFromData(it))
}
}
}
Run Code Online (Sandbox Code Playgroud)
创建一个Sequence<Result>能提供结果的正确方法是什么?
我正在尝试使用Java实现此处所述的非阻塞二进制搜索树.该算法基于单世界CAS,但:
状态和信息字段一起存储在CAS对象中.因此,内部节点使用四个字的存储器.
我决定使用AtomicStampedReference来存储这些数据.问题在于它
通过创建表示"盒装"[引用,整数]对的内部对象来维护标记引用.
这种结构的内部实施:
public boolean compareAndSet(V expectedReference,
V newReference,
int expectedStamp,
int newStamp) {
Pair<V> current = pair;
return
expectedReference == current.reference &&
expectedStamp == current.stamp &&
((newReference == current.reference &&
newStamp == current.stamp) ||
casPair(current, Pair.of(newReference, newStamp)));
}
Run Code Online (Sandbox Code Playgroud)
对定义为:
private static class Pair<T> {
final T reference;
final int stamp;
private Pair(T reference, int stamp) {
this.reference = reference;
this.stamp = stamp;
}
static <T> Pair<T> of(T reference, int stamp) { …Run Code Online (Sandbox Code Playgroud) 给出一个注释
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class MyAnnotation
Run Code Online (Sandbox Code Playgroud)
如何使用此注释查找方法?
这是我得到了多远:
val cls = myObject.javaClass.kotlin
val found = cls.memberFunctions.filter { it.annotations.contains( ??? ) }
Run Code Online (Sandbox Code Playgroud) 只要没有实施额外的方法,Traits就可以很好地与Retrofit配合使用.取决于返回类型RetrofitError: TwitchApi.someMethod: HTTP method annotation is required (e.g., @GET, @POST, etc.).或被java.lang.IllegalArgumentException: TwitchApi.someMethod: Must have either a return type or Callback as last argument.抛出.
有没有办法让改造忽略一个没有注释的方法retrofit.http.GET / PUT / ... ?
public trait SomeApi {
GET("/endpoint")
public fun getSomething(Query("user") user: String): Observable<SomeResponse>
class object {
public fun create(): SomeApi {
val restAdapter = RestAdapter.Builder().setEndpoint("http://localhost").build()
return restAdapter.create<TwitchApi>(javaClass<SomeApi >())
}
}
public fun someMethodThatBreaksRetrofit(user: String) : Int {
return processResponse(getSomething(user))
}
}
Run Code Online (Sandbox Code Playgroud) Buck 支持构建基于 Java 的 Android 项目。有一个项目https://github.com/zserge/buckbone增加了“实验性” Kotlin 支持,但它远未可用。
有没有办法使用 Buck 构建系统构建用 Kotlin 编写的 Android 应用程序?
我发现一个奇怪的默认参数值不是默认值:
class Dog
class DogHouse(dog: Dog)
inline fun <T: Any, A: Any> build(sth: String = "", call: (A) -> T) {}
fun main(args: Array<String>) {
build(call = ::DogHouse)
build(::DogHouse) // This line fails to compile
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
Error:(11, 5) Kotlin: Type inference failed: inline fun <T : Any, A : Any> build(sth: String = ..., call: (A) -> T): Unit
cannot be applied to
(KFunction1<@ParameterName Dog, DogHouse>)
Error:(11, 11) Kotlin: Type mismatch: inferred type is KFunction1<@ParameterName Dog, DogHouse> but String …Run Code Online (Sandbox Code Playgroud)