[REBOL] Re: Declaring variables (or words or whatever)
From: joel::neely::fedex::com at: 3-Jun-2003 14:33
Hi, Steven,
Steven White wrote:
> If I want to declare a variable, just because I like to do things that
> way because it helps me keep track of things,...
>
That concept (in the sense that c-like languages use it) simply
doesn't exist. Values have types, and there's no way to require
that a given word be set only to values of a specific type.
foo: "I have"
foo: 5.0
foo: #toes
foo: %per
foo: 1.0
foo: 'foot
REBOL is perfectly willing for all of those expressions to be
evaluated one after the other.
If, on the other hand, you want to manage the scope of words, and/or
avoid cluttering up the global namespace, you could wrap each script
(or other meaningful unit of code) in an object, so that the explicit
setting of the words during the creation of the object serves both to
initialize them and to give you a consolidated "reminder list" of the
names you intend to use. However, this is purely a matter of choice
and convention.
Comments would also work as well.
That said...
> TP-FILE-ID: make file! %TPLAYER.DAT ;;; (number one)
>
> make file! TP-FILE-ID: %TPLAYER.DAT ;;; (number two)
>
> TP-FILE-ID: %TPLAYER.DAT ;;; (number three)
>
> They all seem to work, that is, if I "print TP-FILE-ID"
> I get the same results, but is one better/more efficient/
> recommended/better style
> etc.?
>
Among those three, the last is the most reasonable, because
>> type? %tplayer.dat
== file!
the value %tplayer.dat is *already* of the FILE! datatype,
so the extra conversions are as pointless as saying
foo: make integer! 3
to obtain an integer result.
The first one does the redundant conversion before setting
the word. The second actually has no effect at all (unless
wrapped in a larger expression which uses the resulting
value) as TP-FILE_ID is already set to the "literal" FILE!
value. The result of that setting operation is a reference
to the same FILE! value, which is then (re-)converted to a
FILE! value. Unless there's a surrounding expression which
does something else with *that* value, it's effectively
discarded when the next expression begins.
HTH!
-jn-
--
----------------------------------------------------------------------
Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446
Counting lines of code is to software development as
counting bricks is to urban development.