Skip to content

Instantly share code, notes, and snippets.

@npryce
npryce / string-char-to-int-pitfall.md
Created January 26, 2019 23:33
string-char-to-int-pitfall

In Kotlin String.toInt() and Char.toInt() do very different things.

>>> "9".toInt()
9
>>> '9'.toInt()
57
>>> 
@npryce
npryce / wrapping-with-after-before.kt
Last active October 31, 2018 09:46
minutest-hooks
fun TestContext<*>.withTestTiming() {
var start: Long? = null
before {
start = System.currentTimeMillis()
}
after {
start?.let {
val timeTaken = System.currentTimeMillis() - it
@npryce
npryce / fixture-with-descriptor.kt
Created October 31, 2018 09:37
Minutest Fixtures with Descriptors
class Fixture {
val tempFile: File
}
fixture { testDescriptor ->
Fixture(System.createTempFile(testDescriptor.fullName.joinToString("/"), ".json"))
}
test {
doSomethingThatWritesTo(tempFile)
@npryce
npryce / reference-to-extension-method-of-nullable-type-via-nonnullable-type-pitfall.md
Created January 31, 2018 15:42
Taking a reference to an extension of a nullable type is possible through the non-nullable type, but probably not what you want!

E.g. the stdlib defines the following function:

fun CharSequence?.isNullOrBlank(): Boolean

You can take a reference to this extension function like this:

val isNullOrBlankFn = CharSequence?::isNullOrBlank
@npryce
npryce / speech_synthesis.kt
Created December 12, 2017 15:34
Kotlin definitions for the browser speech synthesis API
package browser
import org.w3c.dom.events.Event
import org.w3c.dom.events.EventTarget
typealias EventHandler<E> = (E) -> Unit
external val speechSynthesis: SpeechSynthesis
external class SpeechSynthesis : EventTarget {
@npryce
npryce / touch.kt
Created December 12, 2017 15:34
Kotlin definitions for browser touch API
package browser
import org.w3c.dom.Element
import org.w3c.dom.events.UIEvent
typealias TouchId = Int
external interface Touch {
val identifier: TouchId
val target: Element
@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 / layout.kt
Created December 6, 2016 11:44
kotlin-layout-hack
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this