Skip to content

D lang #1

@cyrusmsk

Description

@cyrusmsk

import std.stdio, std.format, std.checkedint;

struct Country {
string name;
int population;
};

// https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population
immutable Country[] countries = [
Country("China", 1_412_600_000),
Country("India", 1_375_586_000),
Country("United States", 333_299_945),
Country("Indonesia", 275_773_800),
Country("Pakistan", 235_825_000),
Country("Nigeria", 218_541_000),
Country("Brazil", 215_424_390),
Country("Bangladesh", 165_158_616),
Country("Russia", 145_100_000),
Country("Mexico", 128_533_664),
];

void main() {
int total;
bool overflow;
foreach (country; countries) {
writeln(country.name, " ", format("%,3?d", '', country.population));
total = opChecked!"+"(total, country.population, overflow);
if (overflow) {
writeln("FAILED!");
return;
}
}
writeln("total ", format("%,3?d", '
', total));
}

// https://dlang.org/phobos/std_checkedint.html
// https://dlang.org/phobos/core_checkedint.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions