evincarofautumn / kitten

A statically typed concatenative systems programming language.
http://kittenlang.org/
Other
1.09k stars 39 forks source link

Conflicting definitions in REPL should overwrite oldest with newest #114

Open kchaloux opened 10 years ago

kchaloux commented 10 years ago

At the moment, it isn't possible to redefine a function in the REPL.

>>> def greet(->) { "Hello!" say }
>>> def greet(->) { "Welcome!" say }
REPL:1:1: error: duplicate definition of sayHi
REPL:2:1: note: also defined here

While this is correct behavior when compiling or running via interpreter, most REPLs allow redefinition of functions in this case. The older definition should be cleared and replaced, so the resulting input should behave as follows:

>>> def greet(->) { "Hello!" say }
>>> greet
Hello!
>>> def greet(->) { "Welcome!" say }
>>> greet
Welcome!
evincarofautumn commented 10 years ago

Note to self: program composition should use right-biased union of definition sets, also removing definitions that had referenced redefined definitions.

daira commented 9 years ago

A warning "Redefining 'greet'." would be nice.

daira commented 9 years ago

BTW, what happens if a previous definition referred to the redefined function and no longer type checks with the new definition?

Oh, you said it should be removed (whether or not it would still typecheck). Hmm, that could end up removing an unexpectedly large set of definitions, requiring the user to have to retype or repaste them.

evincarofautumn commented 9 years ago

We could go more Forthlike, having a dictionary you insert into with def. So if you redefine f, then old-f will be hidden, but definitions pointing to old-f will continue to work. That ordering restriction can be relaxed in source files. We could add a :redef command for the case when you have some definition g that refers to old-f, which you want to patch to call new-f instead. This would also retypecheck, of course, and not commit the patch to the dictionary if checking fails.