AnyDSL / artic

The AlteRnaTive Impala Compiler
MIT License
25 stars 6 forks source link

Allow `use` to import declarations into the current namespace, like in Rust #25

Open Hugobros3 opened 2 months ago

Hugobros3 commented 2 months ago

Artic introduced module support a couple years ago, unfortunately it has not seen much adoption. The oft-cited reason by would-be users is that this module support lacks the ability to import definitions into a namespace, which makes using declarations from others prohibitively verbose (at a minimum you must use a single-letter import like A::).

This PR changes the use syntax to be closer to Rust's, which allow:

PearCoding commented 2 months ago

Is the parent scope/module visible by default or do we have to use super still?

E.g.,

fn foo() = 42;

mod bar {
  fn sup() {
    let v = foo();
    v
  }
}
Hugobros3 commented 2 months ago

No, but this is also the behavior of Rust actually...

https://godbolt.org/z/MxqP15sEn

I nonetheless have a patch that allows for this. I'm tempted to allow ourselves to diverge from Rust here, and allow for that, but we should probably discuss this on call or on Discord at least.

WeiPhil commented 2 months ago

Typically in rust you would just do a use super::* in the module to have access to the parent. While in a single file this seems superfluous I think it helps clarity as to where something unkown comes from when accessing a module of a parent.

PearCoding commented 2 months ago

Seems like it is the standard in Rust, so I would say we stick to the default. Spamming some use super::* everywhere is a small burden to bear :D