Skip to content

Instantly share code, notes, and snippets.

View udalov's full-sized avatar

Alexander Udalov udalov

View GitHub Profile
@udalov
udalov / gist:3645169
Created September 5, 2012 21:31
data class example
data class Result(val x: Int, val y: String)
fun computeSomething(): Result {
return Result(42, "")
}
fun main() {
val (x, y) = computeSomething()
println(x)
println(y)
@udalov
udalov / gist:3820097
Created October 2, 2012 15:24
Multiple constructors workaround
class A() {
var state: Any
}
fun A(state: Any): A {
val a = A()
a.state = state
return a
}
@udalov
udalov / gist:5017161
Created February 22, 2013 22:41
libsndfile usage
#include <cstdio>
#include <vector>
#include <sndfile.h>
using namespace std;
int main() {
SF_INFO sfinfo;
const int BUF_LEN = 2048;
class A { void foo() {} }
class a { }
public class Main {
public static void main(String[] args) {
new A().foo();
}
}
@udalov
udalov / api.kt
Last active August 29, 2015 14:23
Sketch of a more type-safe reflection API for Kotlin
@file:suppress("UNCHECKED_CAST", "BASE_WITH_NULLABLE_UPPER_BOUND", "UNUSED_PARAMETER")
interface KElement
// Class, package, type
interface KDeclarationContainer : KElement {
val allMembers: Collection<KElement>
}
@udalov
udalov / keybase.md
Created February 21, 2016 11:09
KeyBase auth

Keybase proof

I hereby claim:

  • I am udalov on github.
  • I am udalov (https://keybase.io/udalov) on keybase.
  • I have a public key whose fingerprint is 40BF FCAA AA01 B591 916C AAB6 8DA6 620F 79E0 B972

To claim this, I am signing this object:

@udalov
udalov / spec-sealed-data.md
Created March 28, 2016 11:31
Spec: sealed + data

Summary

Allow data classes as sealed class subclasses.

sealed class Base {
    data class Derived(...) : Base()
}

Motivation / use cases

@udalov
udalov / getKType.kt
Last active March 16, 2021 21:54
DEPRECATED example how to obtain KType instance from reified T. Please use "typeOf" since 1.3.40!
import java.lang.reflect.*
import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.KTypeProjection
import kotlin.reflect.KVariance
import kotlin.reflect.full.createType
// --- Interface ---
inline fun <reified T : Any> getKType(): KType =
@udalov
udalov / loadOriginName.kt
Last active March 1, 2018 17:03
Load name of the original anonymous/synthetic class which was copied during inline (KT-21320)
import org.jetbrains.kotlin.serialization.jvm.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
fun loadOriginName(className: String) {
val metadata = Class.forName(className).declaredAnnotations
.filter { it.annotationClass.qualifiedName == "kotlin.Metadata" }
.single() as Metadata
if (metadata.k == 3 && metadata.d1.isNotEmpty()) {
// Synthetic classes generated for lambdas/coroutines
@udalov
udalov / bootstrap-ir.py
Last active September 12, 2019 14:34
Test compilation of the Kotlin project with JVM IR
#!/usr/bin/env python3
import atexit, os, re, shutil, subprocess, sys, time
from pathlib import Path
def to_str(buf):
return "".join(map(chr, buf))
def dump_stdout_stderr(f, result):
f.write("=== STDOUT ===\n\n")