Guide

Coming from Go

What maps directly from Go, the five habits that will trip you up, and the concurrency tools that are not built yet.

Go and TorbScript agree that errors are values and that code has one formatted layout. From there they part: there is no nil, no zero value, and a type says which interfaces it has.

At a glance#

GoTorbScriptNote
x := 1const x = 1 or var x = 1you say whether it can change
(value, error), if err != nilResult<Value, Failure> and ?one return value, and ? passes the failure on
nilValue? (Option<Value>)a missing value is a type
type Shape interface { Area() float64 }trait Shape { fn area(): Float }a trait, much like an interface
satisfying an interface by accidenttype Square with Shape or extenda type says which traits it has
switch v := x.(type)match x { .Circle(radius) => ... }a forgotten case is a compile error
zero valuesnoneevery binding and field gets a value
exported = capitalizedpublicthe first letter means type or value, not visibility
go f()a function returning Task<Value>, and .await()waiting is in the type
chan T, selectChanneldesigned, not runnable yet
gofmttorb formatone layout
go test, t.Errorftorb test, assert(...)a failure prints the expression
[]T, map[K]VList<Item>, Map<Key, Value>the same idea
struct embeddingwith Trait by fielda trait's methods forwarded to a field

What changes in your code#

fn parsePort(text: String): Result<Int, String> {
  const port = Int.tryFrom(text).mapError({ _ => "{text} is not a number" })?
  if port < 1 || port > 65535 {
    return Fail "{port} is not a port"
  }
  port
}

print parsePort("8080")
// prints Ok(8080)

? replaces if err != nil { return err }. The check is still there: on a Fail, ? returns it at once.

What TorbScript does not have#

No nil, so no nil pointer and no nil map. No zero values: nothing is left half built. panic cannot be recovered, because it means a bug. Channel and Stream are designed but do not run yet (Concurrency and streams); Task, .await() and numbers.parallel() run today.

Habits to unlearn#

  • if err != nil after every call. Write ?.
  • A nil map or slice. An empty one is [], always safe to use.
  • Capital letters for visibility. Write public.
  • A type switch. Give the type cases and match on them.
  • A goroutine fired and forgotten. A function that waits returns a Task, which somebody awaits or cancels.
Esc