print("hi, it’s talktalk")
talktalk is a programming language. It kind of looks like Swift or Rust, especially if you don’t know those languages.
It’s got structs, strings, ints, floats, enums,
effects, bools, tuples,
arrays, and
functions.
struct Person {
let name: String
let age: Int
}
"hello there"
1000
1.23
enum Distance {
case yards(Int)
case meters(Int)
}
effect 'log(_ line: String)
handle 'log { line in
print("log: " + line)
resume()
}
'log("hello")
let yes = true
let no = false
yes && !no
(123, true, "hello")
[1, 2, 3]
func increment(x) {
x + 1
}
increment(1)
struct Person { let name: String let age: Int }
syntax
Here, have some math.
ok ok, that was exciting, let’s write a function now
func add(x, y) {
x + y
}
Let's call the function with Ints:
Now let's call it with Strings:
Wow functions are polymorphic. What a world!
We can also define functions with labeled params.
"Ok Alonzo Church" you say, "but do you have like, normal variables?"
We do! I'm getting to it...
let is forever. If you want to change your mind later, say mut.
types
Ok Philip Wadler, maybe you like types? You can specify them if you want.
let a: Int = 1 let b: Float = 2.0
They’ll be checked.
But you can also not specify them and types will still be checked. (Fun fact: an integer literal is happy to be a Float if that's what's needed, so 1 + 2.0 is fine. A string is not.)
Functions can have type annotations as well.
# it's good to be explicit sometimes func identity<T>(_ x: T) -> T { x }
Functions are values too, and they can capture state. A mut variable that a function captures and assigns is shared with it: the compiler keeps it in a Ref, a cell you can also make yourself. A Ref is used as what it holds: r.count(), r.push(3), and r + 1 read and write through it, and r.get() takes the value out. Everything else is a plain value.
You can also use trailing blocks for callback-y stuff.
objects
Ok Alan Kay, maybe you like objects. You know, big bags of state and behavior that are the only correct way to program.
By default, structs get constructors generated automatically. But if your struct is special then you can define a custom constructor with init.
Structs are values. You can't change one unless you're holding it with mut, the only methods that can change one say mut func, and the only fields that can change are declared mut too.
enums / pattern matching
What about enumerations? With attached values even?
enum Response { case ok(String) case redirect(String) case other(Int) } let response = Response.ok("all good here, how are you?")
You can pattern match on enums. Your match expression will be checked for exhaustivity. Try deleting the last arm. i dare u.
Wow. Astounding.. Amazing. Simply... Extraordinary ok fine all modern languages can do that but talktalk can as well is all I'm saying.
You've also got a couple builtin enums, Option and Result. They look like this:
enum Option<T> { case some(T) case none } enum Result<T, E> { case ok(T) case err(E) }
Pretty standard stuff. Int? is short for Option<Int>, and .some(42) knows what enum it belongs to from what's expecting it.
I never promised you flowers. Or wait, i did? Ugh ok here have guard clauses instead, in the form of let else.
Speaking of bailing early, any two-variant enum can short circuit a function (say like, Option or Result). Think rust's ? operator but dumber but simpler. Elegant? One might say. But one might say a lot of things so who knows.
What if you want to just go nuts and damn the torpedoes I know this thing is fine, stop yelling at me compiler, do you even know who my dad is?? For those cases you can use !, which simply unwraps the first variant and panics if it hits the second variant.
protocols
visits Glasgow once what about traits type classes interfaces protocols? For making ad-hoc polymorphism less ad-hoc? Yea we've got those.
Let's write a super basic protocol that lets a type be added to itself.
protocol Addable { func add(to other: Self) -> Self }
How do we make types conform to it? With a lil extend declaration. Think rust's impl Y for X or swift's extension X: Y.
Protocols can also have associated types with their own constraints (basically the associated types must conform to other protocols).
A default method can build on the protocol's own requirements, handing a new method to every conformer at once.
A protocol can require properties too: a let one is read through the protocol, and a mut one can be assigned through it. A struct's field of the same name answers it, and a record with the right fields conforms all by itself.
gadts
Not only can we pronounce "GADTs"1, we can use them.
Each arm of the match knows what T actually is, so eval can give you back Int or String, not T. Because everyone hates T2
effects
What are effects? Great question. I don't know. But i think they're like weird lil functions. Functions that can suspend execution, hand control off somewhere else, then return it. Think like, async/await in other languages, but more generalized.
A handler doesn't have to resume. An affine effect is allowed to cancel() instead, which unwinds everything back to the handler. That's how you get exceptions without having exceptions.
Functions carry their effects as part of their signatures, in a row written [...]. Printing is the 'stdio(write:) effect, sleeping is 'wait(on:until:), and a Ref is 'heap(read:) and 'heap(write:). The compiler figures the row out from the body, so you mostly don't write them.
Hover your mouse over the function names to see their rows.
collections
Arrays. They do what you think.
Subscripts work the way you would think. Unless you don't think they'd work. In which case they work but they don't work the way you don't think they don't work.
Ranges exist too.
There are tuples, with positional access.
Iteration is a protocol. A for walks a Collection where it is, and for n in consume xs uses up anything Iterable, which is the one to conform to when your type makes its elements as it goes.
Anything Iterable gets the helpers you'd expect: map, filter, skip, take, zip, the gang's all here. They're eager, so each one hands you back an array.
print takes anything that converts to text, through an Into<String> protocol. The core types conform, and every struct and enum gets a conformance from its fields unless it writes its own.
strings
Strings are UTF-8, and the String type knows it: every one is well-formed text. Ok Werner Buchholz, you want the bytes? You can call bytes() to get bytes.
A Char is one Unicode scalar: count() counts those, and chars() walks them. To count what you see (extended grapheme clusters, UAX #29, etc. etc.), the unicode package has graphemes(), which means emoji can't tear.
ownership
talktalk has memory semantics. what are they? um, basically: everything has value semantics, sharing is implicit and cheap (refcounted, copy-on-write), and the compiler figures out the retains and releases. You mostly don't have to think about it.
Function parameters borrow by default, so calling a function gives nothing up.
But sometimes a value really is one of a kind: a ticket, a token, a file handle. Mark the type .linear and it must be used exactly once. Not zero times, not two times.
Reading a one-of-a-kind value doesn't use it up, only keeping it does. A function that just looks is lent the value, and the compiler works out which is which from the function's body, so there's nothing to write. A for lends each element too, and whatever it walks is still yours afterwards.
Want to change the elements where they are? for mut. Want to take them and be done with the collection? for coin in consume purse.
When you really do want two things to share one thing, say so with a Ref. It's the one value with identity, so it can even point at itself. Struct values copy: with a plain next field, second would hold the old first, whose next is .none, and the walk below would stop after two steps at 4. Through the cell the two nodes point at each other, and the walk goes round and round.
concurrency
Concurrency is a library, because effects. A Scheduler runs ordinary direct-style tasks and switches between them whenever one pauses.