olix / Mycel / Swift developers
in development

Mycel for Swift developers

let and var, optionals with if let, ??, struct/class/enum and closures work the way you know. The differences are a few spellings, and each one gets an error that names the Swift habit.

Try Mycel in your browser →

Same

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.

optional.mycoptionals and if let, as in Swift · open in playground
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)
output
got 42
6 42
Different

What is different

  • fn instead of func; interpolation is f"v {x}" instead of "\(x)".
  • match x { .a => ... } instead of switch x { case .a: }.
  • 0..3 already excludes the end (so there is no ..<); 0..=3 includes it.
  • extend Int { ... } instead of extension; Char instead of Character; Float instead of Double.
  • guard let is written if x == nil { return } or if let v = x { ... }; defer becomes try { } finally { }.
  • First assignment creates variables (x = 1) without let/var for simple declarations where natural.
ranges.myc0..3 excludes the end, 0..=3 includes it · open in playground
for i in 0..3 { write(f"{i} ") }
print("")
for i in 0..=3 { write(f"{i} ") }
print("")
output
0 1 2 
0 1 2 3 
Habit fix-its

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.

habit.myca Swift habit gets a fix, not a puzzle · open in playground
func double(x: Int) -> Int {
  return x * 2
}
compiler says
error [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 writeMycel says
func f() -> Intfunc 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 / varworks: the same
if let x = y { }works: the same
x?.y, x ?? y, x!works: the same
struct / class / enum / protocol→interfaceworks: the same (protocol is interface)
closures x in ... → x => ...works: x => x * 2
print(a, b)works: the same
deferdefer is Go/Swift — put cleanup in try { } finally { }
CharacterCharacter is Swift — Mycel writes Char
nilworks: the same
Double / String as typesDouble 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.

Packages

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.

Try it

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.

More

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.