Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bson (by spec?) orders elements; this implementation doesn't define order of elements #2

Open
tcoram opened this issue Jul 16, 2013 · 3 comments

Comments

@tcoram
Copy link
Owner

tcoram commented Jul 16, 2013

Need to look closer at spec and see what it takes to order elements.

@dptole
Copy link
Contributor

dptole commented Aug 20, 2014

What order the elements is the binary string itself but, in my opinion, the order don't matter because some languages just force a specific sorting of the elements of an array, leaving no option to the developer.

Lua does but JavaScript don't, so I think it's up to the community to take care of that, even though I think it is not an important issue.

@LinusU
Copy link

LinusU commented Oct 19, 2015

@dptole I cannot possibly think of any language that forces a specific sorting of arrays, mind elaborating a bit?

a = {}

a[1] = "a"
a[2] = "b"
a[3] = "c"

"b" will always be at position 2, the language doesn't force any sorting here?

@dptole
Copy link
Contributor

dptole commented Oct 21, 2015

@LinusU sorry, I didn't express myself well
In Lua we don't have arrays or objects as in JavaScript or others, there is just tables which maps any value to any value.
If you have a table that looks like an array you can change the values of the already defined indexes and the language will not reorder the array for you.

> a = {1,2,3}
> for k, v in pairs(a) do print(k,v) end
1    1
2    2
3    3
> a[2] = 'foo'
> for k, v in pairs(a) do print(k,v) end
1    1
2    'foo'
3    3

But if you have a table that looks like an object then we start getting strange behaviours.

> a = {lua = 'moon', terra = 'earth', marte = 'mars'}
> for k, v in pairs(a) do print(k,v) end
marte   mars
lua     moon
terra   earth -- It was not displayed the way I defined
> a[1] = 'pluto' -- Strange but possible
> for k, v in pairs(a) do print(k,v) end
marte   mars
1       pluto -- I think this item should not appear here
lua     moon
terra   earth

-- Now let's insert them one by one

> a={lua = 'moon'}
> for k, v in pairs(a) do print(k,v) end
lua     moon -- OK
> a['terra'] = 'earth'
> for k, v in pairs(a) do print(k,v) end
lua     moon
terra   earth -- Nice
> a['marte'] = 'mars'
> for k, v in pairs(a) do print(k,v) end
marte   mars
lua     moon
terra   earth -- Lost the order

If we have an issue about the order of the elements and the language has this behaviour we would have to do a little more work to implement something that, in my opinion again, it is not important at all. At least not now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants