Convert array to custom type when passing function argument
Adam D. Ruppe
destructionator at gmail.com
Wed Apr 17 12:48:52 UTC 2019
On Wednesday, 17 April 2019 at 12:20:49 UTC, Stefanos Baziotis
wrote:
> I want to be able to make it work array-like to have compact
> function calls.
> Something like this:
> test([1,2,3]);
This is the "implicit construction" I sometimes talk about....
and D doesn't support it, by design (alas).
There's two options you can do:
1) offer an overload to your function that does the conversion:
void test(Buf!int arr) { /* impl here */ }
void test(int[] arr) { test(Buf!int(arr)); } // just forward to
the other
or
2) Offer a helper construction function you can call, like:
Buf!int toBuf(int[] a) { return Buf!int(a); }
and then you can call the function like
test([1, 2, 3].toBuf); // calling your function at the end
More information about the Digitalmars-d-learn
mailing list