<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hello everyone!<div class=""><br class=""></div><div class="">First of all, I know swift goes into a bit different direction for super type safety, but the problem that is going to describe is kind of common to anybody who use general purpose library (that are not bound to any big UI/non-UI framework).</div><div class=""><br class=""></div><div class="">I’m a game-engine and deep learning developer and I use Swift on the daily basis. The thing I think that Swift really misses is struct protocols. Here is the basic example.&nbsp;</div><div class=""><br class=""></div><div class=""><b class="">Problem:&nbsp;</b></div><div class="">Imagine you do a game with some sort of game engine. That game engine is based on some math lib and you store all vector data (i.e. positions etc) in the something like Point2D from that lib. And then you decided that you need to draw a complex polygons in your game. So you search for a triangulation lib. And you find it, but it uses its own format to describe points, say Triangulation.Point. The thing is that you have a huge buffer of points to triangulate, but you need to map every single one of them to that new format of that lib and than convert it back.&nbsp;</div><div class=""><br class=""></div><div class=""><b class="">Proposal:</b></div><div class="">Wouldn’t it be nice if we could invent a way to globalize types like that?&nbsp;</div><div class=""><br class=""></div><div class="">Imagine you declare something like this in your imaginary triangulation library:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">final</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;public&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures;" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> Vector2D { // or the syntax could be `Vector2D: struct`</span></b></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures;" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> x: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Double</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures;" class="">get</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">set</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></b></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures;" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> y: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Double</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures;" class="">get</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">set</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></b></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">}</b></span></div></div><div class=""><br class=""></div><div class="">And base the algorithm on this one. So you tell the compiler that we will operate on the structs with a known and final layout. Then in your application you only need to do:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">public</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Point2D</span><span style="font-variant-ligatures: no-common-ligatures;" class="">: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Vector2D</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {}</span></b></div></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Types that will conform to such protocols will have some very strict constraints. For example they will have to have the same memory layout (i.e. no protocol variables), stored properties in extensions (that are coming soon) will be also forbidden for such types. The goal is to decrease the need of converting data when you don’t really have to. Those types will have to have the same properties, in the same order, all of them must be stored ones.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">For example we have a bunch of C libs like <b class="">OpenCV</b> or <b class="">dlib</b> and every one of them has its own image format. Every single one has own its pixel struct and you always need to convert between them.&nbsp;</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">I personally think that having something like «type globalization» can improve language ecosystem overall and make things more cross-platform. You can think of it as a typealiasing but without knowing a type you are aliasing to. The compiler will just merge all those types into one during compilation.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Once again: I know this is kind of arguable thing, I just wanted to know your opinion on that.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Thanks for your attention,</div><div style="margin: 0px; line-height: normal;" class="">Andrey Volodin</div></body></html>