Misc syntax questions

Since Moonbit feels Rusty, curious to know if Moonbit aims to align with the following in the future or if the goal is actually not to do it due to particular reasons:

  1. In terms of syntax, it was really great to see the latest blog post that enables implementing traits explicitly:
impl Trait with method(...) { ... }
impl Trait for Type with method(...) { ... }

Does this mean this exists in parallel to interfaces being structural behind the scenes? So we have the choice between both the explicit syntax and the implicit one? (where a struct that has the right methods will automatically type check for this trait)

  1. (Minor syntax difference) explicit trait specification for multiple methods
// We will have to repeat `impl Trait with` for every default method
impl MyTrait with default_method(...) { ... }
impl MyTrait with default_method2(...) { ... }

// We will have to repeat each `impl MyTrait for Type with` 
// And they are not enforced to be in the same block corresponding to a particular trait
impl MyTrait for Type with method1(...) { ... }
impl MyTrait for Type with method2(...) { ... }
impl MyTrait for Type with method3(...) { ... }

impl OtherTrait for Type with method1(...) { ... }
impl OtherTrait for Type with method2(...) { ... }

// We cannot write something like:
impl MyTrait for Type {
  fn method1(...) { ... }
  fn method2(...) { ... }
  fn method3(...) { ... }

impl OtherTrait for Type {
  fn method1(...) { ... }
  fn method2(...) { ... }

=> Is there maybe a reason/advantage they need to be specified with additional blocks, repeating the impl Trait with/for? Or is it possible that the syntax changes in the future?

  1. Multiple and custom attributes (like Rust’s #[])
// Right now we have:
struct Point[N] {
  x: N
  y: N
} derive(Debug)

// Will we be able to have multiple attributes in the future? 
// As well as custom ones when there is a macro system and any information on the vision planned for macros? 
// For example will we be able to do in the future
#[derive(Debug, OtherAttribute)]
struct Point[N] {
  x: N
  y: N
1 Like
  1. Yes, explicit implementation and structural implementation via methods exist in parallel, because they suit different needs. When one is indeed trying to explicitly implement a trait, explicit implementation with its explicitness is preferred. However, if you are a trait owner and want to capture common behavior of existing types, automatic, structural implementation via methods is better, and reduce a lot of unnecessary package dependencies. Once the trait system gets completely stabled, we will explain its design in more details in the doc.

  2. Currently, all toplevel syntax constructs in MoonBit are non-nesting. This makes moving code block around easier, and is more friendly to AI completion

  3. Currently MoonBit does not have a macro system, so attributes are not as useful as in Rust. There is a pragma mechanism in doc comment to mark definitions as deprecated, etc…

1 Like

Awesome thanks a lot for the all the context! Definitely looking forward to hearing more about the vision of AI completion with Moonbit in future blogposts