Completing C code with D style

Salih Dincer salihdb at hotmail.com
Wed Nov 3 21:01:15 UTC 2021


On Wednesday, 3 November 2021 at 20:36:08 UTC, russhy wrote:

> I don't understand why you guys offer OP such 
> complicate/bloated examples, it'll only make things confusing 
> and slow down compilation time with templates and imports, this 
> is not needed at all

I don't like complicated things either. Additional facilities are 
interminable in D :)

For example:

```d
import std.stdio, std.algorithm, std.array;

int main() {
	auto numbers = [-3, 14, 47, -49, -30, 15, 4, -82, 99, 26];
	char negativity, even;

Start:
	bool error;

  	enum sign { negatives = 'n', positives = 'p', both = 'b' }
	write("Would you like in list (n=negatives, p=positives, 
b=both)? ");
	readf(" %c", &negativity);

	switch (negativity) with(sign) {
		case negatives:
			numbers = filter!("a < 0")(numbers).array;
			break;
		case positives:
			numbers = filter!("a > 0")(numbers).array;
			break;
		case both:
		break;
		default:
			error = true;
			goto Error;
	}
	
	enum type { evens = 'e', odds = 'o', both = 'b' }
	write("Would you like in list (e=evens, o=odds, b=both)? ");
	readf(" %c", &even);

	switch (even) with(type) {
		case evens:
			numbers = filter!("!(a % 2)")(numbers).array;
			break;
		case odds:
			numbers = filter!("a & 1")(numbers).array;
			break;
		case both:
		break;
		default:
			error = true;
	}

Error:
	if(error) {
		"Error...".writeln;
		goto Start;
	} else writef("%(%s\n%)", numbers);

	return 0;
}
```
Please try to use auto...


More information about the Digitalmars-d-learn mailing list