Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrey Breslav abreslav

View GitHub Profile
View annotated-lambda.kt
annotation class A
fun main(args: Array<String>) {
val f = @A {}
f.javaClass.methods.filter { it.name == "invoke" && it.parameterTypes.isEmpty() }.forEach {
it.annotations.forEach(::println)
println(it)
println()
}
View function-declaration-error-when-compiling-with-kotlin-12.kt
public class SomeAdapter(val friends: SomeAdapterProvider, val listener: OnItemClickedListener) : RecyclerView.Adapter<SomeAdapter.ViewHolder>() {
trait OnItemClickedListener {
fun onItemClicked(f: Friendship)
}
private inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v), View.OnClickListener {
override fun onClick(v: View) {
listener.onItemClicked(somethings[getPosition()])
}
View ratio.kt
class Ratio(num : Int, denom : Int) {
val numerator: Int
val denominator: Int
{
val theGcd = gcd(num, denom)
numerator = num / theGcd
denominator = denom / theGcd
}
}
View getInstance.md

Calling a class object member from Java:

public class Engine private() {
    class object {
        public fun getInstance(): Engine = Engine()
    }
}
@abreslav
abreslav / kotlin2014-15.md
Last active Aug 29, 2015
Kotlin Project Theme(s) for Open Academy - 2014/15
View kotlin2014-15.md

Kotlin (Programming Language)

Kotlin is a modern statically typed programming language targeting the Java and JavaScript platforms. It is conceived as a "modern language for industry", keeping a balance between flexibility and readability, expressiveness and safety.

Project Overview

A modern programming language is much more than a grammar. Principal susystems of Kotlin include

  • Compiler front-end
  • Lexer/Parser translates text into a syntax tree, reports syntax errors
View Variance.kt
// Why do we need variance
// Invariant class List, just like in Java
class List<T> { /* normal list */ }
fun printContents(list: List<Any>) {
for (item in list) {
println(item)
}
}
View gist:9188294
Red node isn't marked as red: Red node isn't marked as red
java.lang.AssertionError: Red node isn't marked as red
at com.intellij.vcs.log.data.VcsLogJoiner$RedGreenSorter.markRealRedNode(VcsLogJoiner.java:175)
at com.intellij.vcs.log.data.VcsLogJoiner$RedGreenSorter.getFirstSaveIndex(VcsLogJoiner.java:191)
at com.intellij.vcs.log.data.VcsLogJoiner$RedGreenSorter.access$300(VcsLogJoiner.java:161)
at com.intellij.vcs.log.data.VcsLogJoiner.getRedCommitsAndSavedRedIndex(VcsLogJoiner.java:154)
at com.intellij.vcs.log.data.VcsLogJoiner.addCommits(VcsLogJoiner.java:53)
at com.intellij.vcs.log.data.VcsLogDataHolder.smartRefresh(VcsLogDataHolder.java:385)
at com.intellij.vcs.log.data.VcsLogDataHolder.access$2400(VcsLogDataHolder.java:88)
at com.intellij.vcs.log.data.VcsLogDataHolder$12.consume(VcsLogDataHolder.java:668)
View gist:9188277
Ref should exist for this node. Hash: 603812
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:113)
at com.intellij.vcs.log.graph.mutable.GraphBuilder.createBranch(GraphBuilder.java:123)
at com.intellij.vcs.log.graph.mutable.GraphBuilder.addCurrentCommitAndFinishRow(GraphBuilder.java:107)
at com.intellij.vcs.log.graph.mutable.GraphBuilder.append(GraphBuilder.java:165)
at com.intellij.vcs.log.graph.mutable.GraphBuilder.runBuild(GraphBuilder.java:198)
at com.intellij.vcs.log.graph.mutable.GraphBuilder.build(GraphBuilder.java:29)
at com.intellij.vcs.log.data.DataPack.build(DataPack.java:43)
at com.intellij.vcs.log.data.VcsLogDataHolder.smartRefresh(VcsLogDataHolder.java:404)
View sample.kt
class Bar {
fun invoke(x: Int): Int = x
}
class Foo {
val get: Bar = Bar()
}
fun foo () {