Re: length() of an atom
- Posted by jimcbrown (admin) Jul 28, 2010
- 1625 views
What if you had length() return -1 if it was given an atom?
I guess doing abs(length(x)) would be a compromise, but it'd be a pain to have to include std/math.e for what should be a builtin operation...
It might be more painful to have to go through all your code and insert a
if sequence( x ) then
above every
if length( x ) then
or
y = length( x )
But as this is already the case anyways, to avoid crashing, we lose nothing here.
If you really needed old code to crash, you could always do this:
function length(object x) if not sequence(x) then crash("TODO: add eu-lint") end if return eu:length(x) end function
How convenient. So now I have to have my own function to do what Euphoria did for me before.
Why do you need the code to _CRASH_ ?
Also, I have code that does this:
for t=1 to length( ftxt ) do if ftxt[t] = '#' then ...
ftxt should be a sequence, and if it's not, there's a bug. Now you're going to make me test ftxt first? I should probably do that anyway. heh. :)
It will still _CRASH_ ...
length() returns a property (number of elements) of a variable. atoms don't have a length property, so it's inconsistent.
Like Derek said, length() is part of a group of operations which "are by their nature SEQUENCE operations and atoms have nothing to do with it."
Thank you, Derek. :D
It's like getting the "reverse" of an atom. Makes no sense! Yes, we nicely return the atom for some inexplicable reason instead of making the programmer provide a sequence. That's a quirk of the language I guess.
Measuring the length of an atom is like measuring the depth of a square. A square has no depth so you can't measure it, nor will any value be accurate. It is undefined.
I fully agree with you here. From a theoretical viewpoint, a length should only apply to sequences (and similiar objects such as linked lists, arrays, etc).
But you put it best below...
I don't mind quirky behaviors. But don't pass this off as consistent or logical. They're programmer's helpers, at best.
Agreed, and I feel the use as a programmer's helper here significantly outweighs the desire to adhere to theoretical dogma.
I want to know in what case(s) is length(atom) return 1 a good thing.
To borrow Jeremy's example, a function that takes an Euphoria object and serializes it into a memory block for use by a C function.