# The MoonBit Update——0318

**URL:** <https://discuss.moonbitlang.com/t/the-moonbit-update-0318/186>\
**Category:** Announcement\
**Tags:** weekly-update\
**Created:** [March 18, 2024, 9:43am UTC](https://discuss.moonbitlang.com/t/the-moonbit-update-0318/186 "2024-03-18T09:43:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jodie](https://discuss.moonbitlang.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jodie](https://discuss.moonbitlang.com/u/jodie)\
**Post date:** [March 18, 2024, 9:43am UTC](https://discuss.moonbitlang.com/t/the-moonbit-update-0318/186/1 "2024-03-18T09:43:35Z")

</div>

MoonBit is a Rust-like language (with GC support) and toolchain optimized for WebAssembly experience. This is our recent update:

## Language Update

### 1. Experimental support for default method in trait

```auto
trait MyShow {
  repr(Self) -> String
  str (Self) -> String // it has a default implementation
}

impl MyShow::str(self : Self) -> String {
  // default implementation of str
  self.repr()
}

type MyInt Int
fn repr(self:MyInt) -> String {
  self.0.to_string()
}
// Now MyInt implements MyShow now

```

### 2. The type parameters of type definitions can be `_`, which can be used to define phantom types to restrict some logically illegal operations in the program. For example, we want to prevent adding lengths of different units together:

```auto
type Length[_] Int

type Kilometer
type Mile

fn add[T](x: Length[T], y:Length[T]) -> Length[T] {
  Length::Length(x.0 + y.0)
}

let d_km: Length[Kilometer] = Length(10)
let d_mile: Length[Mile] = Length(16)

```

At this point, lengths with different units cannot be directly added:

```rust
fn init {
  add(d_km, d_mile) |> ignore
  // ^ ~~~~~ Error: Expr Type Mismatch
}

```

However, lengths with the same units can be added:

```auto
fn init {
  add(d_km,d_km) |> ignore
  // OK
}

```

### 3. Now, a top-level function without a marked return value is an error.

```auto
fn print_hello() {
// ^ ~~~~~~~~~~ Error:
// Missing type annotation for the return value.
  println("Hello!")
}

```

### 4. Added the bitwise NOT operator.

```auto
fn main {
  println((1).lnot())
}

```

Output:

```auto
-2

```

### 5. Improved the output of `List::to_string/debug_write`.

```rust
fn main {
let l = List::Cons(1, Cons(2, Cons(3, Nil)))
println(l)
}

```

Output:

```rust
List::[1, 2, 3]

```

### 6. Added the `Byte` type.

The byte literals are prefixed by `b`. The following snippet demonstrates its usage:

```rust
fn init {
let b1 = b'a'
println(b1.to_int())
let b2 = b'\xff'
println(b2.to_int())
}

```

More utility methods on `Byte` type are around the corner.

## IDE Update

### 1. Added support for autocompletion of moonbitlang/core.

### 2. Formatting Update：

a. Adjust empty structs, enums, and traits to avoid blank lines.

Before：

```rust
struct A {

}

```

After：

```rust
struct A {}

```

b. Fixed incorrect indentation for `continue`  
c. Fixed issues with semicolons appearing after formatting multiline statements

## Build System Update

### 1. Added the `test_import` field to `moon.mod.json`, which contains dependencies that are only used during testing.

### 2. Optimized the output of `moon test`; by default, it now only outputs information for failed test cases. Use the `moon test -v` command for complete output.
