Standard Library Naming
The standard library should name features in a consistent and clear way. Ideally, the expectations on the naming from developers used to other languages should be met as much as possible.
Cardinality
The standard library should provide immutable and mutable features, while the default should be immutable.
Fuzion should have syntactic sugar for the creation of arrays, lists and maps with pre-defined values. These values could be compile-time constants or run-time variables.
Some languages, such as Python and Go have on single function that can be used on most basic types, such as strings, arrays, maps/dictionaries, etc.:
in Python: len("hello world")
in Go:
x := map[String]int {"one": 1, "two": 2} len(x)
In Python, len()
even works with higher level data types such as
collections.deque
. Similarly, in Go the convention seems to be to
define a Len
function for higher level data types such as
List
and Ring
in the container
part of the
standard library.
In D, strings are a special case of arrays, and the number of elements in an
array is returned by the .length
property. Dynamic arrays and
container types in D additionally have a .capacity
property which
gives the maximum number of elements that can be stored in the container.
Some languages use things such as sizeof
to measure the amount
of memory used by an array in bytes or the amount of memory needed by the pointer
pointing to the collection. The latter seems rather unnecessary.
In other languages, naming of the function used to measure the cardinality of some object differs depending on the type of this object.
Strings
in Java: String.length()
in Kotlin: String.length
in Rust:
let a = String::from("foo"); assert_eq!(a.len(), 3);
Arrays
in Java: array.length
in Kotlin: Array.size
in Rust: arr.len()
in F#: let arr = [| 1; 2; 3; 4; 5; 6 |]
Lists
in Java: ArrayList.size();
in Kotlin: ArrayList.size
Maps, Sets
in Java: Set.size()
in Kotlin: HashMap.size
in Rust:
// for number of elements hashmap.len(); // for maximum number of elements that can be contained without reallocation hashmap.capacity();
Counting
Some languages, such as Kotlin, offer a separate function count
.
Without arguments, this does the same as length or size; however it can be given a
predicate, i.e. a function from a character, or an element of an array, or an entry
of a map to a boolean value. In this case the number of elements for which the
predicate returns true is counted.