Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #42838
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: namespace (was: Forth and "embedded" enviroment) |
| Date | 2015-10-26 11:23 +0000 |
| Organization | Institut fuer Computersprachen, Technische Universitaet Wien |
| Message-ID | <2015Oct26.122343@mips.complang.tuwien.ac.at> (permalink) |
| References | <n0934k$6kh$1@dont-email.me> <n09fqj$8pn$1@dont-email.me> <n0apph$3dl$1@dont-email.me> <n0be4i$o28$1@dont-email.me> <n0jket$d66$1@dont-email.me> |
kristoff <kristoff@skypro.be> writes:
>Today, I have been playing around a little bit with the code I have for
>setting the clock on the STM32F103. I ported the code I originally wrote
>to set the CPUspeed from assembler to Forth.
>
>
>If this would have been in (say) C or arduino (i.e. C++), what I would
>do now is drop the code somewhere on the internet (github), making it a
>seperate library/class.
>That way, the names of the functions I have written would surely not
>interfere with with somebody else might have used.
>
>
>How does one do this in Forth?
>Is there a way to set up different "namespaces". How do you make sure
>you can write code and that the names of the "functions" and "constants"
>do not get in eachother way.
As others have written, you can use wordlists/vocabularies for that.
However, namespaces are the C++ solution for a C++ problem (actually
it's common to all Algol family languages):
If you have the same name defined in two libraries, C++ gives you an
error (unless you use name spaces).
In Forth, if you have two words with the same name in two libraries,
in many cases the program just works. Say, you have:
library1:
: foo ... ;
: bar ... foo ... ;
library2:
: foo ... ;
: boing ... foo ... ;
main program:
require library1
require library2
: main bar boing ;
Here the FOO in BAR refers to the FOO in library1, and the FOO in
BOING refers to the FOO in library2, just as the programmers of these
libraries intended. If you need to access one or both of the FOOs
from the main program, you can arrange that:
main program:
require library1
synonym lib1-foo foo
require library2
synonym lib2-foo foo \ not necessary, just for clarity
: main bar boing lib1-foo lib2-foo ;
In contrast, C++ and its ancestors up to Algol and Fortran give you
error messages when you define FOO twice, so you need name spaces or
something similar to work around that.
One tends to avoid such name clashes in Forth by renaming one of the
FOOs if we can help it, because in debugging we want to be able to
call both FOOs. But for the same reason we prefer renaming to
wordlists: we would have to manage the search order all the time
during debugging.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2015: http://www.rigwit.co.uk/EuroForth2015/
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Forth and "embedded" enviroment kristoff <kristoff@skypro.be> - 2015-10-22 00:23 +0200
Re: Forth and "embedded" enviroment Roberto Waltman <usenet@rwaltman.com> - 2015-10-21 19:19 -0400
Re: Forth and "embedded" enviroment rickman <gnuarm@gmail.com> - 2015-10-21 21:59 -0400
Re: Forth and "embedded" enviroment kristoff <kristoff@skypro.be> - 2015-10-22 15:37 +0200
Re: Forth and "embedded" enviroment rickman <gnuarm@gmail.com> - 2015-10-22 15:43 -0400
Re: Forth and "embedded" enviroment jonjo <jjonethal@yahoo.de> - 2015-10-22 15:26 -0700
Re: Forth and "embedded" enviroment rickman <gnuarm@gmail.com> - 2015-10-23 03:20 -0400
namespace (was: Forth and "embedded" enviroment) kristoff <kristoff@skypro.be> - 2015-10-25 23:20 +0100
Re: namespace Paul Rubin <no.email@nospam.invalid> - 2015-10-25 15:45 -0700
Re: namespace kristoff <kristoff@skypro.be> - 2015-10-25 23:53 +0100
Re: namespace Paul Rubin <no.email@nospam.invalid> - 2015-10-25 16:18 -0700
Re: namespace "Elizabeth D. Rather" <erather@forth.com> - 2015-10-25 14:01 -1000
Re: namespace Paul Rubin <no.email@nospam.invalid> - 2015-10-25 17:40 -0700
Re: namespace "Elizabeth D. Rather" <erather@forth.com> - 2015-10-25 12:50 -1000
Re: namespace kristoff Bonne <kristoff@skypro.be> - 2015-10-26 00:11 +0100
Re: namespace "Elizabeth D. Rather" <erather@forth.com> - 2015-10-25 14:10 -1000
Re: namespace kristoff <kristoff@skypro.be> - 2015-11-03 23:11 +0100
Re: namespace "Elizabeth D. Rather" <erather@forth.com> - 2015-11-03 16:17 -1000
Re: namespace kristoff <kristoff@skypro.be> - 2015-11-07 17:56 +0100
Re: namespace (was: Forth and "embedded" enviroment) hughaguilar96@gmail.com - 2015-10-25 16:31 -0700
Re: namespace (was: Forth and "embedded" enviroment) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-10-26 11:23 +0000
Re: Forth and "embedded" enviroment kristoff <kristoff@skypro.be> - 2015-11-03 12:16 +0100
Re: Forth and "embedded" enviroment rickman <gnuarm@gmail.com> - 2015-11-03 11:12 -0500
Re: Forth and "embedded" enviroment kristoff <kristoff@skypro.be> - 2015-11-04 14:06 +0100
Re: Forth and "embedded" enviroment rickman <gnuarm@gmail.com> - 2015-11-04 09:29 -0500
Re: Forth and "embedded" enviroment Matthias Koch <matthias.koch@hot.uni-hannover.de> - 2015-10-22 11:13 +0200
Re: Forth and "embedded" enviroment kristoff <kristoff@skypro.be> - 2015-10-22 15:54 +0200
Re: Forth and "embedded" enviroment Matthias Koch <matthias.koch@hot.uni-hannover.de> - 2015-10-22 16:42 +0200
csiph-web