Hello Community!
Our development team is planning to add support for variable length lists to URScript.
Variable length list still needs to declare maximum capacity. Runtime dynamic heap allocations are not allowed due to the real-time nature of URScript.
We would like to get your input on use cases from your fields of expertise where this feature would be useful. Additionally we would like to get input for list operations you would need.
Functionality will be introduced in Polyscope 5.15.
Creating lists
Fixed length lists can be created with square bracket operator:
aa = [11, 22, 33, 44, 55, 66, 77]
Both length and capacity of this list is equal to 7
New script function accepts length, and capacity separately:
bb = make_list(length = 7, initial_value = 11, capacity = 20)
Length of this list is 7, but capacity is 20. List can be extended and contracted between 0, and 20 numeric elements
Types of values
Lists can hold any type that URScript supports. This includes complex values created with struct()
keyword.
List can hold only one type of value.
aa = [1, 2, 3.5, 4, 5.5]
bb = make_list(10, struct(p1 = 1, p2 = "text"), 10)
cc = ["a", "b", "c", "d"]
List properties
Length, and capacity properties can be accessed by built-in methods
a = aa.length()
b = aa.capacity()
Modifying lists
Lists can be modified with built-in methods:
aa.append(88) # add element to the end of the list, length increases, exception thrown if capacity exceeded
a = aa.pop() # pop last element of the list, and decrease length
aa.insert(1, 22.5) # inserts new value on index 1, increase list length, and shift elements after inserted value
aa.clear()
Assignments between lists
List can be assigned only to existing list of greater or equal capacity to the length of source list
aa = [1, 2, 3, 4, 5, 6] # aa.length() == 6, aa.capacity() == 6
bb = make_list(5, 0, 100) # bb.length() == 5, bb.capacity() == 100
aa = bb # aa.length() == 5, aa.capacity() == 6, aa.to_string() == "[0, 0, 0, 0, 0]"
aa = [1, 2, 3, 4, 5, 6]
bb = aa # bb.length() == 6, bb.capacity() == 100
Accessing structured data types
List can hold structs (aka complex data types). All structs in the list have to be of exactly the same type.
aa = make_list(10, struct(p1 = 1, p2 = "text"), 10)
a = aa[4].p1 # a = 1
b = aa[4].p2 # b = "text"
aa[3].p1 = 22.5
aa[4] = struct(p1 = 99, p2 = "different text")
Error handling
All errors are reported as Runtime Exceptions, and stop the program.
length()
, and capacity()
methods on the lists aid in defensive programming to avoid stopping the program.
Limitations
We already have a list of limitations on what lists would not be able to do:
- lists can be passed to, and returned from functions only as copy by value
- list elements can’t change type
- if list is returned from a function or list method, then target list have to be earlier initialized with enough capacity
- list of lists is not supported as this is how matrices are implemented in URScript
We’re waiting for your feedback.