AdeptLanguage / Adept

The Adept Programming Language
GNU General Public License v3.0
119 stars 9 forks source link

[bug] Namespaced types don't behave as expected #58

Closed IsaacShelton closed 3 years ago

IsaacShelton commented 3 years ago

Namespaced types don't behave as expected when accessed via using namespace

main.adept

import "other.adept"

// Another issue is that 'custom\Thing' and 'Thing' are not treated as the same
// type, even though they are.
// It seems like the two types are only equal depending on whether the root file
// "uses" the namespace. The same problem happens with a recently fixed struct/union "using namespace"
// bug. Somehow the IRGEN stage will need information about which file "object" it is being evaluated in.
#default include_namespace false

#if include_namespace
    using namespace custom
#end

func main {
    // Even though 'custom\Thing' is the same type as 'Thing',
    // the non-namespaced name isn't accessible and so the two are treated as incompatible
    x custom\Thing = createThing()
}

other.adept

// This line seems to have no effect on type comparisons (since it's not in the root file)
using namespace custom

struct custom\Thing ()

func createThing() custom\Thing {
    t POD Thing
    return t
}

Probably somewhat related to #57

e.g. Something like this needs to be able to work

using namespace custom

namespace custom {
    alias XType = int
    alias YType = XType
    struct Thing ()

    func createThing(x XType, y YType) Thing {
        t POD Thing
        return t
    }
}

For something like this, namespace resolution rules will have to be so that "used" namespaces are tested before the global namespace, and then have parameter/argument types resolved before IRGEN stage.

All functions within the file will need to have their parameter types and return types (and for structs/unions: their field types, etc.) converted to match the global namespaced name of the type. So the above would be implicitly transformed into something like this:

using namespace custom

namespace custom {
    alias XType = int
    alias YType = custom\XType
    struct Thing ()

    func createThing(x custom\XType, y custom\YType) custom\Thing {
        t POD Thing
        return t
    }
}
IsaacShelton commented 3 years ago

After trying many things, the the headache of using namespace and contextual names is just not worth it in my opinion.

A proper implementation of using namespace would have to account for all of the following:

Also using namespace as it was previously intended was broken and poorly implemented.

So using namespace and other ways of having contextual names have been removed in 1bd44a96f59cae12c5150f1352cb8f1d9810baff