How does one cast to an array type?

Andrea Fontana nospam at example.com
Thu Oct 17 12:36:15 UTC 2019


On Thursday, 17 October 2019 at 12:19:03 UTC, Vinay Sajip wrote:
> Are arrays and objects part of a unified type system? 
> Specifically, if I do
>
> void foo(Object x) {
>     if (typeid(x) == typeid(Object[])) {
>         auto a = cast(Object[]) x;
>     }
> }
>
> I get a compilation error:
>
> onlineapp.d(3): Error: cannot cast expression x of type 
> object.Object to Object[]
>
> What's the way to achieve the cast, which should be safe given 
> the typeid check?
>
> Thanks and regards,
>
> Vinay Sajip

Did you try templates? It seems easier:
https://run.dlang.io/is/BbkTCw

Andrea



More information about the Digitalmars-d-learn mailing list