Question about Walter's Memory DisAllocation pattern

Parke via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 26 19:45:11 PDT 2015


Hi,

I have a question about Walter's DConf keynote and the Memory
DisAllocation pattern.

http://dconf.org/2015/talks/bright.html

The following example is from the slides of Walter's talk.

----

auto toString(uint u) {
  static struct Result {
    this(uint u) {
      idx = buf.length;
      do {
        buf[--idx] = (u % 10) + '0';
        u /= 10;
      } while (u);
    }
    @property bool empty() { return idx == buf.length; }
    @property char front() { return buf[idx]; }
    void popFront() { ++idx; }
    char[uint.sizeof * 3] buf;
    size_t idx;
  }
  return Result(u);
}

import std.stdio;

void main() { writeln(toString(28)); }

----

My question is:

Does use of this pattern in D require that the size of the Result
struct be known by the compiler at compile time?

Or, perhaps more precisely:  Does the caller of toString need to know
the size of the struct that toString will return?

In the above example, buf's length is uint.sizeof * 3.  But what if
buf's length was a function of u (and therefore only known at
run-time), rather than a function of uint.sizeof?

Thanks!

-Parke


More information about the Digitalmars-d mailing list