What is the same
if x { } else { }, nil, // comments and the struct Foo { } shape work as in Go. for i in 0..3 { } and for x in items { } cover what for ... range does.
struct Point {
let x: Int
let y: Int
}
p = Point(x: 1, y: 2)
for i in 0..3 {
print(i)
}
if p.x < p.y {
print("x is smaller")
}output0 1 2 x is smaller
What is different
- First assignment creates variables (
x = 5) orlet x = 5replacesx := 5; a type follows a colon:var x: Int = 1. The decimal type isFloat, notfloat64. fninstead offunc;print(x)instead offmt.Println(x);match x { 1 => ... }instead ofswitch;x += 1instead ofx++.go f()isspawn f().deferis replaced bytry { } finally { }.- A Mycel file is a module, so there is no
packageline. - Structs are immutable value types: their fields are
let, and the compiler says so if you writevar(shown below). Use aclassfor mutable state.
struct Point {
var x: Int
}compiler sayserror [E0227]: struct field `x` cannot be `var` --> structs.myc:2:3 | 2 | var x: Int | ^^^^^^^^^^ why: structs are immutable value types; this keeps copies and equality predictable fix: declare it `let x`, or make Point a class if it needs mutable state 1 error, 0 warnings
Every Go habit and what Mycel says
Mycel's compiler recognises words and symbols from other languages. Instead of a generic syntax error you get the language it came from and the Mycel spelling. The full list for Go is below; rows marked works are valid Mycel as you already write them.
fmt.Println("hi")compiler sayserror [E0101]: `fmt.Println` is Go — Mycel writes `print(...)`
--> habit.myc:1:1
|
1 | fmt.Println("hi")
| ^^^
why: this word comes from another language
fix: see `mycel help habits`
1 error, 0 warnings| You write | Mycel says |
|---|---|
if x { } else { } | works: the same |
nil | works: the same |
// comments | works: the same |
struct Foo { } | works: the same shape |
for i := range / for ... range | works: for i in 0..3 { } (and for x in items) |
x := 5 | := is Go — Mycel writes let x = 5 |
func f() {} | func is Go/Swift — Mycel writes fn f() { ... } |
fmt.Println(x) | fmt.Println is Go — Mycel writes print(x) |
var x int | types follow a colon: var x: Int = 1 |
switch x { case 1: } | switch — Mycel writes match x { 1 => ... } |
x++ | x++ — Mycel writes x += 1 |
go f() | go is Go — Mycel writes spawn f() |
defer f() | defer is Go/Swift — put cleanup in try { } finally { } |
package main | package is Go/Java — a Mycel file is a module; no package line |
int / string / float64 / bool types | float64 is Go — Mycel's decimal type is Float |
Every row is tested in the compiler, and mycel help habits prints the same list. See also the overview on the Mycel page.
Using Go packages: planned (coming in Phase D)
A compatibility engine so a Mycel program can use Go packages is planned, and the package manager ply already recognises the install flags. The planned flag is --go. Installing packages from other languages is marked as coming until Phase B lands, and Mycel itself is not released: there is nothing to download yet. The playground runs the real compiler in your browser with no packages.
Run the first example yourself
The Mycel playground opens in your browser. It runs the real compiler in a sandbox with no file, network or storage access, and nothing is sent to a server. For a guided walk through the language, read Learn Mycel.
Other starting points
Mycel is written so a program reads the same whichever language you learned first. Pick the guide closest to yours, or take the full tour.