11 karma · joined Jul 20, 2012 (14y ago) · 19 submissions
comment 3mo ago · link · parent
comment 3y ago · link · parent
comment 3y ago · link · parent
comment 3y ago · link · parent
ref ubyte[n*2] requires_ptr_twice_as_large_as_input(uint n)
(
const ref ubyte[n] input,
ref ubyte[n*2] output,
) {
output[0..n] = input[0..n];
output[n..2*n] = input[0..n];
return output;
}
void main()
{
ubyte[10] input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
ubyte[20] output;
requires_ptr_twice_as_large_as_input!(10)(input, output);
import std.stdio;
writeln(output);
assert(output == input ~ input);
}comment 4y ago · link · parent
import std.sumtype;
void main(){
struct A { int a; }
struct B { string b; }
struct C { bool c; }
alias TaggedUnion = SumType!(A, B, C);
auto tgu = TaggedUnion(B("hi"));
int i = tgu.match!(
(A a) => a.a,
(B b) => b.b.length,
(C c) => c.c * 5
);
assert(i == 2);
}
If you miss out a `match` handler for any of A, B, C you get a compile-time error. Or you can use a generic handler which will be instantiated for any type not explicitly handled.
https://dlang.org/phobos/std_sumtype.htmlcomment 14y ago · link · parent
If this is 'armchair criticism' why do Go's designers say they are considering adding generics support?
comment 14y ago · link · parent
There are tons of other interesting uses for macros. Obviously I'd rather a language had safer equivalents to a C preprocessor.
comment 14y ago · link · parent
This requires some flow analysis, but brings real benefit and safety.
comment 14y ago · link · parent
`string` is immutable. You can use immutable everywhere if you like. C++ doesn't even have transitive const.
> templates are still templates
With constraints, unlike C++.
> hard-to-solve threading problems are just as hard to solve
Have you looked at D 2.0? Globals are thread-local by default, unlike C++. Implicit sharing is disallowed, unless immutable.
> region pointers
It's early days (perhaps I don't fully get region/borrowed pointers yet) but D's `scope` parameter keyword seems very similar. It prevents an argument escaping the function call.
> lack of nulls
This is the big one. Unfortunately D doesn't seem to have an answer on this. I understand Rust uses option/sum types for this, which seems like a great idea.