<div dir="ltr"><div><div><div><div><div><div><div>Many APIs like OpenGL take arrays where the atomic unit is multiple elements long. For example, a buffer of coordinates laid out like <br><br></div><span style="font-family:monospace,monospace">:[Float] = [ x1, y1, z1, x2, y2, z2, ... , xn, yn, zn ]</span><br><br></div>I want to be able to define <i>in Swift</i> (i.e., without creating and importing a Objective C module) a <span style="font-family:monospace,monospace">struct</span> that preserves the layout, so that I can do <span style="font-family:monospace,monospace">withMemoryRebound(to:capacity:_)</span> or something similar and treat the buffer as <br><span style="font-family:monospace,monospace"><br></span></div><div><span style="font-family:monospace,monospace">struct Point <br>{<br></span></div><div><span style="font-family:monospace,monospace">    let x:Float, <br></span></div><div><span style="font-family:monospace,monospace">        y:Float, <br></span></div><div><span style="font-family:monospace,monospace">        z:Float<br>}<br></span></div><div><span style="font-family:monospace,monospace"><br></span></div><span style="font-family:monospace,monospace">:[Point] = [ point1, point2, ... , pointn ]</span><br><br></div>The memory layout of the <span style="font-family:monospace,monospace">struct</span> isn’t guaranteed, but will the layout be guaranteed to be in declaration order if I use a tuple inside the <span style="font-family:monospace,monospace">struct</span> instead?<br><br></div><span style="font-family:monospace,monospace">struct Point <br>{<br></span></div><span style="font-family:monospace,monospace">    let _point:(x:Float, y:Float, z:Float)<br><br></span></div><div><span style="font-family:monospace,monospace">    var x:Float <br>    {<br></span></div><div><span style="font-family:monospace,monospace">        return self._point.x<br>    }<br></span><br><div><span style="font-family:monospace,monospace">    var y:Float <br>    {<br></span></div><span style="font-family:monospace,monospace">        return self._point.y<br>    }<br><br></span><div><span style="font-family:monospace,monospace">    var z:Float <br>    {<br></span></div><span style="font-family:monospace,monospace">        return self._point.z<br>    }<br></span></div><div><span style="font-family:monospace,monospace">}</span><br><br></div>This is an ugly workaround, but I can’t really think of any alternatives that don’t involve “import something from Objective C”. I am aware that the implementation of structs currently lays them out in declaration order, but I’m looking for something that’s actually defined in the language.<br><br><br></div>