Skip to content

Instantly share code, notes, and snippets.

npryce /
Last active Oct 17, 2016
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 /
Last active Jul 30, 2017
Kotlin/Java interop method overloading pitfall

Given a Java library that defines overloaded methods for primitive and boxed types:

class X {
    public void m(int value) { ... }
    public void m(Integer value) { ... }
npryce /
Created Sep 18, 2016
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 / WasabiKrouton.kt
Last active Mar 8, 2016
Using Krouton (typesafe routing & reverse routing) with Wasabi (Sinatra-like web server library for Kotlin)
View WasabiKrouton.kt
import com.natpryce.krouton.*
import io.netty.handler.codec.http.HttpMethod
import io.netty.handler.codec.http.HttpMethod.GET
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 / metaphor.bib
Created Oct 14, 2015
Metaphor Bibliography
View metaphor.bib
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
npryce / react-for-mocha.js
Last active Sep 10, 2015
Testing react components with Mocha and NodeJS
View react-for-mocha.js
// React decides if it has a DOM on loading, so if we always need to
// initialise JSDOM before loading it.
var testdom = require('testdom');
// But React hates you even more than that... it caches the rendered component
// in the DOM and if the same type of component with the same key is rendered at
// into the same element, then it uses the cached instance, not the just rendered
// instance. Even if the props are different. So the instance being tested will
// not be exercised.
npryce /
Last active Aug 29, 2015
Refactoring to Asynchronous in JavaScript

Note: the latest version of this document is at

Here's some code that gets and uses a value from a synchronous call or built in data structure:

function to_be_refactored() {
    var x;
 x = get_x();
npryce / Makefile
Last active Aug 29, 2015
Makefile to bootstrap and build a Purescript project
View Makefile
npm_bin:=$(shell npm bin)
all: $(outdir)/js/HelloWorld.js
$(outdir)/js/HelloWorld.js: src/HelloWorld.purs
npryce /
Last active Aug 29, 2015
How to create a Log4J Logger for unit testing Java code that logs
LoggingEvent loggedEvent = null;
private Logger capturingLogger() {
return new Logger("testing") {
level = Level.ALL;
repository = mock(LoggerRepository.class);
npryce /
Last active Aug 29, 2015
public interface Prickable {
void prick();
boolean isBleeding();
public interface Ticklable {
void tickle();
boolean isLaughing();