tomhrr / dale

Lisp-flavoured C
BSD 3-Clause "New" or "Revised" License
1.02k stars 48 forks source link

Allow defining functions and variables of outer namespace when inside namespace #162

Open porky11 opened 7 years ago

porky11 commented 7 years ago

Shouldn't be that difficult, I think Example:

(import cstdio)

(namespace test
  (def num (struct extern ((value int))))
  (def .+ (fn extern (retval num) ((a num) (b num))  ;;define plus for root-namespace
    (setf (:@ retval value) (+ (@: a value) (@: a value))))))

(def main (fn extern-c void (void)
  (let ((x test.num ((value 1)))
        (y test.num ((value 2))))
    (printf "%i" (+ x y))))) ;;plus not defined. 

I don't want to use the namespace, to since some other variables from the namespace test are already defined in current namespace. I don't want to write test.+ every time. I may define the function outside of namespace. Problem: Macros won't be able to define something in differnet namespaces (also see my other idea in a pull-request #161 )

tomhrr commented 7 years ago

Allowing the definition of bindings in a namespace outside of the current scope is pretty counterintuitive. As mentioned, the core problem here (and in #162) is that named values need to be within a specific namespace to avoid collisions, whereas the functions that work with those values would preferably be within the root namespace because they can take advantage of overloading. I think this is better dealt with by fixing the qualified import problem identified in

130, so that the functions alone can be imported, and so that the

qualifications are retained on subsequent imports.

porky11 commented 7 years ago

but if only functions are imported (let ((x test.num ((value 1))) …) won't work anymore

tomhrr commented 7 years ago

The type can also be explicitly imported along with the functions, though, in which case the above will work.

porky11 commented 7 years ago

I think I understand, what you mean. Everything has to be imported explicitely. There will still be no easy way to only add functions on enum (like + and |) into the main namespace, but being able to call other funcitons without having to use the namespace.

porky11 commented 7 years ago

Some other problem, when it is not possible to define something in outer namespace, is defining special functions like init and destroy. They have to be defined in root namespace, else they won't work. Even using namespaces seems not to help. When defining new types with init-functions and destructors, I would either have to close the namespace to define the special functions and reopen it afterwards (very verbrose and confusing, especially when using subnamespaces), or I have to seperate the type definition from the function definitions, which may also be bad design in some cases.