What is the same
A class Foo { } has the same shape. if (x) { } else { } works, and the parentheses are optional. while (x) { }, && || ! and try { } catch e { } all work.
class Counter {
var count = 0
fn bump() -> Int {
self.count += 1
return self.count
}
}
c = Counter()
while c.bump() < 3 {
print("looping")
}
print(f"count is {c.count}")outputlooping looping count is 3
What is different
- No boilerplate: write statements directly at the top level, or define
fn main() { ... }. - First assignment creates variables (
x = 5); types follow the name:let x: Int = 5, or inferredlet x = 5. Decimals areFloat, notdouble. System.out.println(x)isprint(x);nullisnil; nonew: writeFoo().- Methods are
fn f() { ... }; there is novoidkeyword, and return types are inferred when omitted. for (int i = 0; ...; i++)becomesfor i in 0..3 { ... }andi++becomesi += 1.- Inheritance is
class B: A { ... }, notextends/implements.publicis spelledpub. - A value that may be missing is typed
Int?and the compiler makes you handle it before use.
fn find(names: [String], wanted: String) -> Int? {
for i, n in names {
if n == wanted { return i }
}
return nil
}
names = ["ana", "bo", "chen"]
print(find(names, "bo"))
print(find(names, "zed"))output1 nil
Every Java 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 Java is below; rows marked works are valid Mycel as you already write them.
public static void main(String[] args) {
System.out.println("hi")
}compiler sayserror [E0101]: `public` is Java — Mycel writes `pub` (and no `static void main` is needed: just write statements)
--> habit.myc:1:1
|
1 | public static void main(String[] args) {
| ^^^^^^
why: this word comes from another language
fix: see `mycel help habits`
1 error, 0 warnings| You write | Mycel says |
|---|---|
System.out.println(x) | System.out.println is Java — Mycel writes print(x) |
public static void main | no boilerplate: write statements directly, or fn main() { ... } |
int x = 5; | types follow the name: let x: Int = 5 (or just let x = 5) |
String / double / boolean as types | double is Java — Mycel's decimal type is Float |
new Foo() | new is JavaScript/Java — Mycel writes Foo() |
void f() | void is Java — Mycel writes fn f() { ... } (no return type needed) |
null | null is JavaScript/Java — Mycel writes nil |
i++ | x++ — Mycel writes x += 1 |
for (int i = 0; ...; i++) | Mycel writes for i in 0..3 { ... } |
extends / implements | extends is Java — Mycel writes class B: A { ... } |
class Foo { } | works: the same shape |
if (x) { } else { } | works: the same (parentheses are optional) |
&& || ! | works: the same |
while (x) { } | works: the same |
try { } catch (e) { } | works: catch e { ... } |
Every row is tested in the compiler, and mycel help habits prints the same list. See also the overview on the Mycel page.
Using Java packages: planned (coming in Phase C)
A compatibility engine so a Mycel program can use Java packages is planned, and the package manager ply already recognises the install flags. The planned form is ply install --java com.google.code.gson:gson:2.11.0. 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.