What is the same
let / var, if let x = y { }, x?.y, x ?? y, x!, nil, struct / class / enum, print(a, b), and closures (x => x * 2) all work. Swift's protocol is called interface.
var maybe: Int? = 42
if let n = maybe {
print(f"got {n}")
}
var total = 0
for n in [1, 2, 3] { total += n }
print(total, maybe ?? 0)outputgot 42 6 42
What is different
fninstead offunc; interpolation isf"v {x}"instead of"\(x)".match x { .a => ... }instead ofswitch x { case .a: }.0..3already excludes the end (so there is no..<);0..=3includes it.extend Int { ... }instead ofextension;Charinstead ofCharacter;Floatinstead ofDouble.guard letis writtenif x == nil { return }orif let v = x { ... };deferbecomestry { } finally { }.- First assignment creates variables (
x = 1) withoutlet/varfor simple declarations where natural.
for i in 0..3 { write(f"{i} ") }
print("")
for i in 0..=3 { write(f"{i} ") }
print("")output0 1 2 0 1 2 3
Every Swift 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 Swift is below; rows marked works are valid Mycel as you already write them.
func double(x: Int) -> Int {
return x * 2
}compiler sayserror [E0101]: `func` is Go/Swift — Mycel writes `fn name(args) { ... }`
--> habit.myc:1:1
|
1 | func double(x: Int) -> Int {
| ^^^^
why: this word comes from another language
fix: see `mycel help habits`
1 error, 0 warnings| You write | Mycel says |
|---|---|
func f() -> Int | func is Go/Swift — Mycel writes fn f() -> Int { ... } |
"\(x)" interpolation | \(x) is Swift — Mycel writes f"v {x}" |
guard let x = y else { return } | guard is Swift — Mycel writes if x == nil { return } or if let v = x { ... } |
switch x { case .a: } | switch — Mycel writes match x { .a => ... } |
for i in 0..<n | ..< is Swift — Mycel's 0..3 already excludes the end (0..=3 includes it) |
extension Foo { } | extension is Swift — Mycel writes extend Int { ... } |
let / var | works: the same |
if let x = y { } | works: the same |
x?.y, x ?? y, x! | works: the same |
struct / class / enum / protocol→interface | works: the same (protocol is interface) |
closures x in ... → x => ... | works: x => x * 2 |
print(a, b) | works: the same |
defer | defer is Go/Swift — put cleanup in try { } finally { } |
Character | Character is Swift — Mycel writes Char |
nil | works: the same |
Double / String as types | Double is Swift — 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 Swift packages: planned (coming in Phase D)
A compatibility engine so a Mycel program can use Swift packages is planned, and the package manager ply already recognises the install flags. The planned flag is --swift. 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.