Skip to content

Instantly share code, notes, and snippets.

@npryce
npryce / unit-versus-unit-question-mark-pitfall.md
Last active March 24, 2017 13:41
Unit? vs Unit vs ?. operator
override fun close() = client?.disconnect()

Return type of 'close' is not a subtype of the return type of the overridden member 'public abstract fun close(): Unit defined in java.lang.AutoCloseable'

Unit? is a supertype of Unit, not a subtype.

@npryce
npryce / optional-interfaces-and-flow-typing.md
Last active December 18, 2016 01:30
Kotlin - make interfaces of an object available without temporary variables

Some libraries define optional interfaces that have to be discovered by dynamic type checking and can only used after casting to the optional interface type.

For example, a "ScriptEngine" provided by the Java scripting API only provides methods for evaluating scripts.

val js : ScriptEngine = ScriptEngineManager().getEngineByName("nashorn")
js.eval("print('hello, world');") 

But the "nashorn" script engine is specified to also implement the Invocable interface, that allows the host program to invoke functions defined by a script, and the Compilable interface, that allows the host program to compile a script and then execute the compiled form.

@npryce
npryce / Makefile
Created December 16, 2016 13:33
Pandoc Makefile example
MARKDOWN_SRC:=$(shell find doc -name '*.md')
DIAGRAM_SRC:=$(shell find doc -name '*.plantuml')
MARKDOWN_FORMAT=markdown+fenced_code_blocks+fenced_code_attributes+grid_tables+footnotes
# Order is significant when multiple CSS files
CSS_SRC:=styles/style.css
SITE_HTML=$(MARKDOWN_SRC:doc/%.md=out/site/%.html) $(DIAGRAM_SRC:doc/%.plantuml=out/site/%.png)
SITE_CSS=$(CSS_SRC:styles/%=out/site/%)
@npryce
npryce / add-to-string-pitfall.md
Last active October 17, 2016 09:56
Another Kotlin string pitfalls

Don't write an extension method that implement the plus operator with String arguments. If you forget to import it, the code will compile, but use string append instead.

public final class String {
  ... public final operator fun plus(other: kotlin.Any?): kotlin.String { /* compiled code */ }
}

So, always write a conversion function from String to your own type, then implement + on that type.

@npryce
npryce / kotlin-null-tostring-pitfall.md
Created September 18, 2016 21:14
A Kotlin pitfall w.r.t. nulls and toString
val x : SomeType? = ... // Note: nullable

val s1 : String? = x?.toString()

val s2 : String? = x.toString()

The expressions s1 and s2 are subtly different. When x is null, s1 will evaluate to null, while s2 will evaluate to the string "null".

@npryce
npryce / wip.py
Created May 28, 2011 20:17
Decorator to mark tests as work in progress for Python's Nose testing framework
from functools import wraps
from nose.plugins.attrib import attr
from nose.plugins.skip import SkipTest
def fail(message):
raise AssertionError(message)
def wip(f):
@wraps(f)
@npryce
npryce / WasabiKrouton.kt
Last active March 8, 2016 11:22
Using Krouton (typesafe routing & reverse routing) with Wasabi (Sinatra-like web server library for Kotlin)
import com.natpryce.krouton.*
import io.netty.handler.codec.http.HttpMethod
import io.netty.handler.codec.http.HttpMethod.GET
import org.wasabi.app.AppServer
import org.wasabi.http.Request
import org.wasabi.http.Response
import org.wasabi.http.StatusCodes
import org.wasabi.interceptors.Interceptor
import org.wasabi.routing.RouteHandler
@npryce
npryce / NoisyThreadingPolicy.java
Last active December 12, 2015 03:28
Noisy threading policy for jMock
private static class NoisyThreadingPolicy implements ThreadingPolicy
{
private final Thread testThread = Thread.currentThread();
@Override public Invokable synchroniseAccessTo(final Invokable mockObject)
{
final StackTraceElement[] creationStack = stackTrace();
return new Invokable()
{
@npryce
npryce / metaphor.bib
Created October 14, 2015 10:06
Metaphor Bibliography
@article{Deignan2005,
abstract = {With the benefit of hindsight, it is now possible to see that one of the most important themes in the study of language to emerge in the 20 th century was developed, not by linguists, but primarily by philosophers of language such as Wittgenstein and Grice and anthropologists such as Malinowski and Rosch. This theme involves, among other things, rejection of sharply defined category boundaries and adoption instead of systems of categories built by analogy around prototypes. Central and typical examples of linguistic categories are usually easy to identify, but boundaries between categories are fuzzy grey areas on a cline, rather than sharp divisions. Metaphor is the most prototypical example of linguistic analogy, so a corpus-based study of metaphor will be a theme of central interest. In the linguistic paradigm shift just mentioned, Aristotelian-Leibnizian theories of meaning requiring the satisfaction of necessary and sufficient conditions were rejected. Instead, meaning in