Skip to content

Instantly share code, notes, and snippets.

@ilya-g
ilya-g / cancellationTest.kt
Last active December 21, 2018 06:42
Cancellation support interceptor
class JobCancellationInterceptor(val originalInterceptor: ContinuationInterceptor?) :
AbstractCoroutineContextElement(ContinuationInterceptor),
ContinuationInterceptor {
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> =
CancellableCheckContinuation(continuation).let {
originalInterceptor?.interceptContinuation(it) ?: it
}
class CancellableCheckContinuation<T>(val continuation: Continuation<T>) : Continuation<T> {
@ilya-g
ilya-g / gson-singletons.kt
Last active November 3, 2016 05:26
In order not to instantiate singletons with gson
import com.google.gson.*
fun <T : Any> GsonBuilder.registerSingletonInstance(instance: T): GsonBuilder =
registerTypeAdapter(instance.javaClass, InstanceCreator { instance })
fun main(args: Array<String>) {
// val gsonBuilder = GsonBuilder().registerTypeAdapterFactory(object : TypeAdapterFactory {
// override fun <T: Any> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
// val c: Class<T> = type.rawType as Class<T>
// return c.kotlin.objectInstance?.let { instance ->
@ilya-g
ilya-g / appendListAndElement.cs
Last active November 12, 2015 18:43
Resolving between element and list
public static class TestResolve
{
public static List<T> Append<T>(this IEnumerable<T> list, T element) { throw new NotImplementedException(); }
public static List<T> Append<T>(this IEnumerable<T> list, IEnumerable<T> elements) { throw new NotImplementedException(); }
public static void Usage()
{
var list = new List<List<String>>();
var element = new List<String>();
package syntax.flagEnums
private val TODO: Nothing
get() = throw UnsupportedOperationException()
public open class PatternOptions(public val value: Int = 0) {
// the only meaning declarations
public object CASE_INSENSITIVE : PatternOptions(0x02)
public object COMMENTS : PatternOptions(0x04)
public object MULTILINE : PatternOptions(0x08)
@ilya-g
ilya-g / GenericBenchmark.cs
Created January 22, 2015 22:35
Interface generic methods call time microbenchmark
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace GenericBencmark
{
class Program
{
public static CastLookup<TKey, TElement> Cast<TKey, TElement>(this ILookup<TKey, TElement> lookup)
{
return new CastLookup<TKey,TElement>(lookup);
}
public class CastLookup<TKey, TElement>
{
private readonly ILookup<TKey, TElement> lookup;
public CastLookup(ILookup<TKey, TElement> lookup)