Skip to content

Instantly share code, notes, and snippets.

@npryce
npryce / string-char-to-int-pitfall.md
Created Jan 26, 2019
string-char-to-int-pitfall
View string-char-to-int-pitfall.md

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

>>> "9".toInt()
9
>>> '9'.toInt()
57
>>> 
View wrapping-with-after-before.kt
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 Oct 31, 2018
Minutest Fixtures with Descriptors
View fixture-with-descriptor.kt
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 Jan 31, 2018
Taking a reference to an extension of a nullable type is possible through the non-nullable type, but probably not what you want!
View reference-to-extension-method-of-nullable-type-via-nonnullable-type-pitfall.md

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 Dec 12, 2017
Kotlin definitions for the browser speech synthesis API
View speech_synthesis.kt
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 Dec 12, 2017
Kotlin definitions for browser touch API
View touch.kt
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
View unit-versus-unit-question-mark-pitfall.md
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 Dec 18, 2016
Kotlin - make interfaces of an object available without temporary variables
View optional-interfaces-and-flow-typing.md

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 Dec 16, 2016
Pandoc Makefile example
View Makefile
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 Dec 6, 2016
kotlin-layout-hack
View layout.kt
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