23. The @ operator
Mini adds exactly one operator to Kotlin: a binary @, in the spirit of Python's @ (PEP 465). It is the matrix-multiplication operator. Nothing in the standard library defines it; you give your own types an operator fun matmul.
Not Kotlin
Programs that use @ are not valid Kotlin and the Kotlin compiler cannot read them. Everything else in Mini is.
By the end: define the operation behind @, predict its precedence and distinguish a binary operator from an annotation or a label. This is a Mini syntax extension, so it is the point where these examples leave standard Kotlin syntax.
Define it, use it
a @ b means a.matmul(b). The function must be an operator fun matmul(other: X): R, as a member or an extension, with exactly one parameter.
class Vec(val x: Int, val y: Int) {
override fun toString() = "($x, $y)"
}
class Matrix(val a: Int, val b: Int, val c: Int, val d: Int) {
operator fun matmul(o: Matrix): Matrix =
Matrix(a * o.a + b * o.c, a * o.b + b * o.d, c * o.a + d * o.c, c * o.b + d * o.d)
operator fun matmul(v: Vec): Vec = Vec(a * v.x + b * v.y, c * v.x + d * v.y)
override fun toString() = "[$a $b; $c $d]"
}
// an extension works too: the dot product of two vectors
operator fun Vec.matmul(o: Vec): Int = x * o.x + y * o.y
fun main() {
val m = Matrix(1, 2, 3, 4)
val v = Vec(5, 6)
println(m @ m)
println(m @ v)
println(v @ v)
println(m @ m @ v)
var p = Matrix(1, 1, 0, 1)
p @= p
p @= p
println(p)
}Press Run. A 2x2 matrix times a matrix, a matrix times a vector and a dot product, all with the same operator, chosen by the type of the right operand (matmul can be overloaded). The last lines show @=:
a @= bcallsa.matmulAssign(b)if there is one, otherwise it meansa = a @ bfor avar, the same rule as*=andtimesAssign.@has the precedence of*,/and%and is left-associative:a + b @ cisa + (b @ c)anda @ b @ cis(a @ b) @ c.
A small experiment
Powers of the Fibonacci matrix give Fibonacci numbers. Change n and watch the output:
class M(val a: Long, val b: Long, val c: Long, val d: Long) {
operator fun matmul(o: M): M = M(a * o.a + b * o.c, a * o.b + b * o.d, c * o.a + d * o.c, c * o.b + d * o.d)
}
fun fib(n: Int): Long {
var result = M(1, 0, 0, 1) // the identity
var base = M(1, 1, 1, 0)
var k = n
while (k > 0) {
if (k % 2 == 1) result = result @ base
base = base @ base
k /= 2
}
return result.b
}
println((1..10).map { fib(it) })
println(fib(80))Where @ is not an operator
@ is the binary operator only after a complete operand and with whitespace on both sides. @Name with no space after it is always an annotation (@Ann, @get:Ann, @file:Ann), and labels (return@label, this@Outer, break@loop) keep their Kotlin meaning. A type without an operator fun matmul gives the error no operator matmul for T, and the call is an ordinary call that works in every memory mode and on every backend.
The reference section is Mini and Kotlin/JVM: Matrix operator.
Checkpoint
How is a + b @ c grouped? Does using @ change who owns the result? Is a @b another spelling of matrix multiplication?
Reasoning
It groups as a + (b @ c). Ownership follows the same rules as an ordinary matmul call. Whitespace on both sides distinguishes the binary operator; a @b is not a product.
Where to go next
Continue with From notebook to native project to put the pieces into a source tree with repeatable checks. Further references:
- Install Mini and build a project with the
minitool. - The memory guide and the diagnostics reference for everything about Strict mode.
- How it works if you want to know how the compiler does it.
- The playground and the notebook for your own experiments.