Why is D unpopular?
Siarhei Siamashka
siarhei.siamashka at gmail.com
Fri May 20 12:45:07 UTC 2022
On Friday, 20 May 2022 at 09:26:58 UTC, bauss wrote:
> On Friday, 20 May 2022 at 07:01:09 UTC, Siarhei Siamashka wrote:
>>
>> Go intentionally randomizes the order of iteration over
>> elements from its associative arrays, so that the
>> unpredictable nature of it is much more visible to the
>> developers and can be caught by unit tests.
>>
>
> I'm not sure how a unittest can catch it, because even if the
> order is randomized then it can in theory end up being ordered.
D unittest (save as "main_test.d" and run as "rdmd -unittest
main_test.d"):
```D
unittest {
// Create an associative array
auto a = ["Alice": true, "Bob": true, "Charlie": true];
// Iterate over the associative array and save keys
string[] s;
foreach (k, _ ; a)
s ~= k;
// If the order is preserved, then the first retrieved name will
be "Alice"
assert(s[0] == "Alice");
}
void main() {
}
```
Go unittest (save as "main_test.go" and run as "go test
main_test.go"):
```Go
package main
import "testing"
func TestAssocArrayOrder(t *testing.T) {
// Create an associative array
a := map[string]bool{"Alice": true, "Bob": true, "Charlie": true}
// Iterate over the associative array and save keys
var s []string
for k, _ := range a {
s = append(s, k)
}
// If the order is preserved, then the first retrieved name will
be "Alice"
if s[0] != "Alice" {
t.Fatalf("The first returned key was not 'Alice'! The actual
order: %v", s)
}
}
```
In Go this unittest will sporadically fail and the problem will
be detected reasonably fast. Of course, assuming that the unit
tests are run regularly.
More information about the Digitalmars-d
mailing list