Skip to content

Commit

Permalink
Clarify initialization of containers
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Nov 1, 2023
1 parent b385d41 commit 31270b5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/pages/libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,8 @@ are agnostic to the input container type.

Spans are also known as "fat pointer" or "slice" in some other languages.

Remarks: A span initialized from a list of unnamed fields points to an array of the list elements.

### spanT

```nelua
Expand Down Expand Up @@ -2877,6 +2879,8 @@ A vector has the following semantics:
otherwise the behavior is undefined, in case this is needed then try the `sequence` library.
* Any failure when growing a vector raises an error.

Remarks: A vector initialized from a list of unnamed fields is filled with the list elements.

### vectorT

```nelua
Expand Down Expand Up @@ -3069,6 +3073,8 @@ thus the list itself is passed by reference by default (like lua tables again).
A sequence is typically used as a more efficient table that
can hold only sequences of a fixed value type.

Remarks: A sequence initialized from a list of unnamed fields is filled with the list elements.

### sequenceT

```nelua
Expand Down
6 changes: 5 additions & 1 deletion lib/sequence.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ thus the list itself is passed by reference by default (like lua tables again).
A sequence is typically used as a more efficient table that
can hold only sequences of a fixed value type.
Remarks: A sequence initialized from a list of unnamed fields is filled with the list elements.
]]

require 'memory'
Expand Down Expand Up @@ -56,7 +58,9 @@ require 'span'
end
return false, string.format("no viable conversion from '%s' to '%s'", x.type, sequenceT)
end, function(node)
return node.tag == 'InitList' and types.ArrayType(T, #node)
if node.is_InitList and #node > 0 and not node:find_child_with_field('is_Pair') then
return node.tag == 'InitList' and types.ArrayType(T, #node)
end
end)]#

--[[
Expand Down
2 changes: 2 additions & 0 deletions lib/span.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Spans are especially useful for making functions with arguments that
are agnostic to the input container type.
Spans are also known as "fat pointer" or "slice" in some other languages.
Remarks: A span initialized from a list of unnamed fields points to an array of the list elements.
]]

## local function make_spanT(T)
Expand Down
6 changes: 5 additions & 1 deletion lib/vector.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ A vector has the following semantics:
* It should never be passed by value while being modified,
otherwise the behavior is undefined, in case this is needed then try the `sequence` library.
* Any failure when growing a vector raises an error.
Remarks: A vector initialized from a list of unnamed fields is filled with the list elements.
]]

require 'memory'
Expand Down Expand Up @@ -46,7 +48,9 @@ require 'span'
end
return false, string.format("no viable conversion from '%s' to '%s'", x.type, vectorT)
end, function(node)
return node.tag == 'InitList' and types.ArrayType(T, #node)
if node.is_InitList and #node > 0 and not node:find_child_with_field('is_Pair') then
return node.tag == 'InitList' and types.ArrayType(T, #node)
end
end)]#

--[[
Expand Down
2 changes: 1 addition & 1 deletion tests/sequence_test.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ end

do -- braces initializer
local seq: sequence(integer) = {}
assert(#seq == 0 and seq.impl ~= nilptr)
assert(#seq == 0 and seq.impl == nilptr)
seq:destroy()
seq = {1,2,3}
assert(#seq == 3 and seq[0] == 0 and seq[1] == 1 and seq[2] == 2 and seq[3] == 3)
Expand Down

0 comments on commit 31270b5

Please sign in to comment.