Example of Rust code
Tove
tove at fransson.se
Fri Aug 10 06:04:49 PDT 2012
On Friday, 10 August 2012 at 12:32:28 UTC, bearophile wrote:
>
> This second D version uses the same class definitions, but
> allocates the class instances on the stack. The code is bug
> prone and ugly. The other disadvantages are unchanged:
>
>
> void main() {
> import std.stdio;
> import std.conv: emplace;
> import core.stdc.stdlib: alloca;
>
> enum size_t size_Val = __traits(classInstanceSize, Val);
> enum size_t size_Plus = __traits(classInstanceSize, Plus);
> enum size_t size_Minus = __traits(classInstanceSize, Minus);
>
> Val e1 = emplace!Val(alloca(size_Val)[0 .. size_Val], 5);
> Val e2 = emplace!Val(alloca(size_Val)[0 .. size_Val], 3);
> Val e3 = emplace!Val(alloca(size_Val)[0 .. size_Val], 1);
> Plus e4 = emplace!Plus(alloca(size_Plus)[0 .. size_Plus],
> e2, e3);
> Minus ex2 = emplace!Minus(alloca(size_Minus)[0 ..
> size_Minus], e1, e4);
>
> writeln("Val: ", eval(ex2));
> }
>
> Probably there are ways to improve my D versions, or to write
> better versions.
>
> Bye,
> bearophile
I think version 2 would be the easiest one to improve, by
including a combined emplace/alloca convenience function in
Phobos for this common use-case.
See the technique used in:
http://www.digitalmars.com/d/archives/digitalmars/D/run-time_stack-based_allocation_166305.html
"auto Create(void* buf=alloca(frame_size))"
More information about the Digitalmars-d
mailing list