Convert array to custom type when passing function argument

Stefanos Baziotis sdi1600105 at di.uoa.gr
Wed Apr 17 15:23:06 UTC 2019


On Wednesday, 17 April 2019 at 12:48:52 UTC, Adam D. Ruppe wrote:
>
> This is the "implicit construction" I sometimes talk about.... 
> and D doesn't support it, by design (alas).
>

Sorry if this has been asked again, I didn't find anything.
Do we know the reason why it is not supported?

> 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

Yes, I had done the 2), which means for 50 functions, I have 
another 50 functions.
Because of the constructor these are one-liners but still, it's 
kind of ugly.

I didn't know about the 3) thanks! I think it's not a very good 
solution for this
kind of problems but cool otherwise.

- Stefanos




More information about the Digitalmars-d-learn mailing list