Skip to main content
24 votes
Accepted

In Python, why isn't it a syntax error when a list of strings is defined without comma separators?

The reason the first example does not error is because Python will automatically concatenate adjacent string literals. This happens inside and outside of lists. As mentioned in the Python ...
lyxal's user avatar
  • 1,825
20 votes

What are the advantages of strings and character arrays being different?

Different definitions of “character” In Swift, the Character type isn’t a single Unicode codepoint (that’s UnicodeScalar), nor ...
Bbrk24's user avatar
  • 9,117
14 votes

Why might a language avoid reallocation?

There is potentially unknown number of variables pointing to ("referencing") the same Java array. It cannot be "reallocated" because these references may end up pointing to nothing ...
Audrius Meškauskas's user avatar
13 votes
Accepted

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

I wouldn't store them as arrays, but you can certainly implement such a syntax efficiently. If you translate x[0] to x & 1 ...
ruarq's user avatar
  • 146
11 votes

What are some syntax options for iterating over arrays?

Functional languages may have a higher-order function (not a list method!) which takes a function/lambda/operator and one (or more!) arrays, and iterates over them. E.g. APL and BQN have Each (...
Adám's user avatar
  • 3,156
11 votes

What are the implications of implementing variable-length arrays?

I'd say one of the reasons is that it was a rather niche feature with a very natural syntax (which you usually try to avoid). Most times I've seen VLAs used in code, it was by novice programmers who ...
abel1502's user avatar
  • 2,537
10 votes

What are the advantages of strings and character arrays being different?

An array must have a fixed stride, so that the memory address of the ith element equals the address of the start of the array plus i times the stride. If you really want an array of characters, so ...
kaya3's user avatar
  • 20k
10 votes

What are the advantages of strings and character arrays being different?

Encapsulation Treating strings as character arrays entails a belief that a character array is an adequate model for a string - not just good enough now, but good enough that you won't want to be able ...
Karl Knechtel's user avatar
9 votes

What are some syntax options for iterating over arrays?

for-in This is the approach taken by Swift and Python: for el in arr { print(el) } JS does something similar, but for historical reasons it uses ...
Bbrk24's user avatar
  • 9,117
9 votes

What are some syntax options for iterating over arrays?

For each function Some languages have a ‘forEach‘ function on their lists that takes a list and a lambda, and applies that lambda to each item. For example, Kotlin: ...
Seggan's user avatar
  • 2,743
9 votes

How can a language statically ensure a required index exists in a list?

Dependent Types A Dependent Type is one whose members can be described by a predicate depending on other values. In your case, it is ...
Longinus's user avatar
  • 1,853
8 votes

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

It would encourage inefficient code 90% of the time when you're doing individual bit lookups on an int, you're doing something "wrong". E.g., this would encourage those who don't feel ...
Rydwolf Programs's user avatar
8 votes
Accepted

What are the advantages of strings and character arrays being different?

Separate string methods or going via arrays A disadvantage of a string type is that the methods available to manipulate arrays cannot just be used for strings. Instead, the language must either ...
Adám's user avatar
  • 3,156
8 votes

What are the advantages of strings and character arrays being different?

Resizing with Bounds Checking and Constant-Time Size Lookup The most common alternatives do not have all three. A fixed-length array is not resizable. And C-style arbitrary-length, null-terminated ...
Davislor's user avatar
  • 1,062
8 votes
Accepted

What should a variable-size contiguous collection be called?

For what it's worth, the Wikipedia page about this data structure calls it a dynamic array, growable array, resizable array, dynamic table, mutable array, or array list I prefer the term "array ...
kaya3's user avatar
  • 20k
8 votes
Accepted

How could C implement first class arrays?

I've abused the preprocessor to implement first-class fat pointers, like @mousetail suggested, but a bit different: ...
Gavin D. Howard's user avatar
8 votes

Why might a language avoid reallocation?

Java couldn't allow this because of how its memory is laid out. There are four pools, each of increasing size. Every time an object is created, it is added to the smallest pool, in a contiguous manner....
phyrfox's user avatar
  • 221
7 votes

How could C implement first class arrays?

Fat pointers One could define an array pointer as: struct Array { int length, int itemLength, void* contents } This matches how arrays are stored in ...
mousetail's user avatar
  • 8,531
7 votes

What are the advantages of strings and character arrays being different?

Handling Encoding Arrays of raw data don't, generally speaking, have an encoding. Strings can be in UTF-8 or any number of other formats. In Ruby, a String object ...
Silvio Mayolo's user avatar
7 votes
Accepted

What are the implications of implementing variable-length arrays?

VLAs were added to C99, but made optional in C11. The reason they were made optional doesn't appear to be in public documents. The initial proposal to add VLA to C (N637) cited numerical computing as ...
Gilles 'SO- stop being evil''s user avatar
7 votes

Why would Short String Optimization not apply to dynamic arrays?

I suspect the difference is that strings are typically immutable, whereas dynamic arrays are mutable. Even if a list is short enough to apply this optimisation, it might soon get long enough that it ...
kaya3's user avatar
  • 20k
7 votes
Accepted

Why would Short String Optimization not apply to dynamic arrays?

Because the number of elements that could be stored in a small vector optimisation is rarely worth it, and the small vector optimisation is not free. Strings are allocated from single bytes which ...
Jack Aidley's user avatar
6 votes

What are some syntax options for iterating over arrays?

Functor fmap For repetitive (but not only) tasks over a structure like an Array, I like the Functional Programming point of view: map/fmap syntax The goal of it is pretty similar to foreach in ...
Stephane Rolland's user avatar
6 votes

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

Strictly speaking, an "array" is a specific kind of data structure where elements are stored at contiguous, equally-spaced memory addresses. The smallest addressable unit of memory is ...
kaya3's user avatar
  • 20k
6 votes
Accepted

To what degree is it practical to let objects be stored packed?

This should not become a language feature, but an implementation detail of some parts of your library. Design the APIs of these parts so that they hide this detail, e.g. let the ...
Ralf Kleberhoff's user avatar
5 votes

What should a variable-size contiguous collection be called?

C++ uses std::vector for contiguous variable-size containers to distinguish from std::array (fixed size) and ...
chrisaycock's user avatar
5 votes

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

It privileges one form of bit access over others Accessing a word as a bit vector can be useful in some circumstances, but even more useful is the ability to pack and unpack general bit fields. Things ...
Pseudonym's user avatar
  • 4,991
5 votes

Why might a language avoid reallocation?

In C, the memory location of the object affected by calling realloc may change, meaning you have to update your pointers to the object after calling it. The ...
occipita's user avatar
  • 569
4 votes

What are some syntax options for iterating over arrays?

for range https://go.dev/tour/moretypes/16 for index, value := range data { fmt.Printf("2**%d = %d\n", index, value) }
OscarRyz's user avatar
  • 199
4 votes

What are some syntax options for iterating over arrays?

Backwards foreach PHP uses this: foreach ($arr as $el) { ... } I think there might also be a language that uses something like ...
naffetS's user avatar
  • 1,057

Only top scored, non community-wiki answers of a minimum length are eligible