On Fri, Oct 04, 2019 at 11:43:34AM -0700, H. S. Teoh via Digitalmars-d-learn 
wrote:
> On Fri, Oct 04, 2019 at 06:34:40PM +0000, Dennis via Digitalmars-d-learn 
> wrote:
> > On Friday, 4 October 2019 at 18:30:17 UTC, IGotD- wrote:
> > > What if you pass a static array to a function that expects a
> > > dynamic array. Will D automatically create a dynamic array from
> > > the static array?
> > 
> > No, you have to append [] to create a slice from the static array.
> 
> Actually, it *does* automatically convert the static array to a slice.
[...]

Here's an actual working example that illustrates the pitfall of this
implicit conversion:

-----
        struct S {
                int[] data;
                this(int[] _data) { data = _data; }
        }
        S makeS() {
                int[5] data = [ 1, 2, 3, 4, 5 ];
                return S(data);
        }
        void func(S s) {
                import std.stdio;
                writeln("s.data = ", s.data);
        }
        void main() {
                S s = makeS();
                func(s);
        }
-----

Expected output:
        s.data = [1, 2, 3, 4, 5]

Actual output:
        s.data = [-2111884160, 32766, 1535478075, 22053, 5]


T

-- 
MSDOS = MicroSoft's Denial Of Service

Reply via email to