Declaring variables (or words or whatever)
[1/3] from: Steven:White:ci:bloomington:mn:us at: 3-Jun-2003 13:20
I'm having a terrible time getting out of my old ways of thinking.
If I want to declare a variable, just because I like to do things that
way because it helps me keep track of things, which of these ways is
recommended, or they equivalent?
TP-FILE-ID: make file! %TPLAYER.DAT ;;; (number one)
... or ...
make file! TP-FILE-ID: %TPLAYER.DAT ;;; (number two)
... or ...
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.?
Thank you.
Steven White
City of Bloomington
2215 W Old Shakopee Rd
Bloomington MN 55431-3096
USA
952-563-4882 (voice)
952-563-4672 (fax)
[swhite--ci--bloomington--mn--us]
[2/3] 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)
<<quoted lines omitted: 3>>
> 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.
[3/3] from: greggirwin:mindspring at: 3-Jun-2003 14:30
Hi Steven,
SW> I'm having a terrible time getting out of my old ways of thinking.
SW> If I want to declare a variable, just because I like to do things that
SW> way because it helps me keep track of things, which of these ways is
SW> recommended, or they equivalent?
SW> TP-FILE-ID: make file! %TPLAYER.DAT ;;; (number one)
SW> ... or ...
SW> make file! TP-FILE-ID: %TPLAYER.DAT ;;; (number two)
SW> ... or ...
SW> TP-FILE-ID: %TPLAYER.DAT ;;; (number three)
SW> They all seem to work, that is, if I "print TP-FILE-ID" I get the same
SW> results, but is one better/more efficient/recommended/better style
SW> etc.?
I'd use #3 for all general cases. While I don't agree *entirely* with
the Agile motto of "Do the simplest thing possible", in this case it
works. REBOL understands the value directly, so there is no need to do
anything special.
In #2 you're just discarding the value returned by MAKE, so it's
entirely redundant.
Now, #1 doesn't behave exactly like #3, so that's the other thing to
consider, and what is one of the hardest things to remember about
REBOL when coming from other languages. How you define values, and how
you use them, needs to be considered. E.g.
print f-1: make file! f-2: f-3: %TPLAYER.DAT
f-4: %TPLAYER.DAT
print equal? f-1 f-2
print same? f-1 f-2
print same? f-2 f-3
print same? f-2 f-4
append f-2 "XXX"
print [f-1 f-2 f-3 f-4]
-- Gregg
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted