dusklang / dusk

The Dusk Programming Language
Other
1 stars 1 forks source link

Namespaces #37

Closed ghost closed 2 years ago

ghost commented 5 years ago

Any time you can use '.' in the language to reference names, you should be able to bring those names into scope (taking a page out of Jai's book). For example:

fn main() {
    // modules
    use cpp.std.vector
    my_vec := vector[i32].new()

    // methods
    use my_vec.push_back
    push_back(12)

    // field access (imagine std::vector has an `x` field)
    use my_vec.x
    x += 13

    // everything
    use my_vec.*
    clear()

    // just some stuff
    use cpp.std.{optional, tuple, experimental.{cool_cpp23_feature, awesome_cpp56_feature}}
    // ...code that uses the above types...

    Foo :: struct { a: i32 }
    Bar :: struct { foo: Foo, b: i32, use foo.* }
    bar := Bar { foo: Foo { a: 43 }, b: 45 }
    print("{}", bar.a + bar.b)
}

I think use should basically work just like Rust's, it should just work in more places. It should be noted that this is a bit trickier in Dusk than in Rust because everything is so expression-oriented. But I think it's tractable. We just have to detect the final name(s) in the chain of .s, and apply that to the expression whenever it's referenced.

zachwolfe commented 2 years ago

This one falls under the "duh, I'm going to add this eventually" category