Convert array to custom type when passing function argument

Stefanos Baziotis sdi1600105 at di.uoa.gr
Wed Apr 17 12:20:49 UTC 2019


I have a custom Buf struct (working as a simple vector)

struct Buf(T) {
     size_t cap, len;
     T *data;

     @nogc
     this(T[] arr) {
         reserve(arr.length);
         foreach(item; arr) {
             push(item);
         }
     }
     ...
};

And I have a function like this:

void test(Buf!(int) buf) {
}

I want to be able to make it work array-like to have compact 
function calls.
Something like this:
test([1,2,3]);

Currently I have the constructor shown and with that I do:
test(Buf!(int)([1,2,3]);
which is compact but can it be done differently? Meaning, if a 
function takes
a Buf!(T), can it also take a T[] and construct a Buf!(T) using 
some constructor?

Thanks in advance!


More information about the Digitalmars-d-learn mailing list