<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Thanks for doing the work to collect links related to this topic! I am planning to give them a read.</div><div class=""><br class=""></div>You may be interested in the proposal I am working on for automatic protocol forwarding. <div class=""><br class=""></div><div class="">The first draft was posted to the list last week: <a href="https://github.com/anandabits/swift-evolution/blob/automatic-protocol-forwarding/proposals/NNNN-automatic-protocol-forwarding.md" class="">https://github.com/anandabits/swift-evolution/blob/automatic-protocol-forwarding/proposals/NNNN-automatic-protocol-forwarding.md</a>. It will give you an idea of where the proposal is going, although it is somewhat out of date at this point. The motivation section is also not fully fleshed out. Several good suggestions came up during the discussion leading to some important design changes.<div class=""><br class=""></div><div class="">I am working on a significantly enhanced second draft right now. It will include examples showing how it can be used to achieve a similar effect to `newtype` (as well as several other examples). It won’t be quite as concise syntactically as it is designed to support many use cases, not just the `newtype` use case. A future enhancement to that proposal might allow more concise syntax to better support the `newtype` use case.<br class=""><div class=""><div class=""><br class=""></div><div class="">I am hoping to have the second draft ready to share soon. </div><div class=""><br class=""></div><div class="">Matthew</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 5, 2016, at 10:11 AM, Grzegorz Adam Hankiewicz via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hello, newcomer here.<br class=""><br class="">The other day I was reading about <a href="https://www.reddit.com/r/swift/comments/3zbk64/type_math/" class="">https://www.reddit.com/r/swift/comments/3zbk64/type_math/</a> and given the verbose solutions decided to pitch a possible language improvement. But after reading through the archives it seems that this idea has already been pitched before:<br class=""><br class="">* Proposal: newtype feature for creating brand new types from existing types<br class=""><br class="">- <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001821.html" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001821.html</a><br class=""><br class="">- <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001826.html" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001826.html</a><br class=""><br class="">Some time later:<br class=""><br class="">* Epic: Typesafe calculations<br class=""><br class="">- <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151228/004735.html" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151228/004735.html</a><br class=""><br class="">Since this is already known I would like to talk about this potential feature in order to address the perceived lack of community interest.<br class=""><br class="">Recently I’ve been bitten by a lack of this feature for code dealing with database serialisation. If a database table is modelled as an object it will most likely use integers or longs to represent primary and foreign keys. An example would be:<br class=""><br class="">struct SomeTable {<br class=""> var primaryKey: Int64<br class=""> var otherTableForeignKey: Int64<br class=""> var yeatAnotherForeginKey: Int64<br class="">}<br class=""><br class="">Unfortunately the types can be mixed, one can assign the value from otherTableForeignKey to primaryKey without impunity. Using the reddit proposed struct as separate type would help here:<br class=""><br class=""> public struct RefTablePk {<br class=""> private let value: Int64<br class=""> }<br class=""><br class=""> public struct SomeTablePk {<br class=""> private let value: Int64<br class=""> }<br class=""><br class=""> struct RefTable {<br class=""> var primaryKey: RefTablePk<br class=""> }<br class=""><br class=""> struct SomeTable {<br class=""> var primaryKey = SomeTablePk(value: 0)<br class=""> var otherTableForeignKey = RefTablePk(value: 0)<br class=""> }<br class=""><br class=""> var a = SomeTable()<br class=""> a.primaryKey = SomeTablePk(value: 3)<br class=""> a.otherTableForeignKey = a.primaryKey // Fails, good!<br class=""> print(a.primaryKey.value)<br class=""><br class="">So far so good. The solution gets more hairy when one attempts to use such fake types for mathematical operations because the new type doesn’t inherit possible operations done on the parent type it is based of:<br class=""><br class=""> public struct Euros {<br class=""> private let value: Double<br class=""> }<br class=""><br class=""> public struct Dollars {<br class=""> private let value: Double<br class=""> }<br class=""><br class=""> var account = Euros(value: 100)<br class=""> var bill = Euros(value: 42)<br class=""> account = account - bill<br class=""><br class="">The last line won’t compile: Binary operator ‘-‘ cannot be applied to two ‘Euros’ operands. The immediate solution would be to add variants of all the usual operators for every type, which quickly gets tedious and verbose. On top of this, each *new* type is generating code, which is actually not desirable. The ideal would be for the compiler to pretend Euros or RefTablePk are different types, yet use their parent type at the binary level. This needs a specific syntax to teach the compiler which existing methods/operations are allowed on the new fake types and which aren’t. These new distinct types would *borrow* previous implementations.<br class=""><br class="">Other examples of possible use for such fake types (note again: these exist only at compilation time and generate no extra code) would be user input validation. Strings coming from *the exterior* (whatever that might be) could use a distinct type of TaintedString instead of the usual String, and would require a validation or converter function to produce a correct String, or maybe a ValidatedString for better distinction. Similarly for a security library it would be useful to mark Strings as encrypted. The EncryptedString would be tracked by the compiler to prevent mistakes or uses where plaintext strings are expected. One bug I recently had the pleasure to solve was mixing time units, some APIs use seconds, other use milliseconds, both store the values as longs. The *fix* was to wrap all primitive type access inside objects with explicit methods having the unit in their signature. Effective, but tedious and verbose.<br class=""><br class="">The typealias jumped out of the swift language documentation while I was reading it, but it is actually the opposite of the desired behaviour. The current type alias doesn’t prevent one from mixing these new fake types so it has effectively the same value as a user comment. Also, having seen <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0011-replace-typealias-associated.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0011-replace-typealias-associated.md</a> it seems that the keyword won’t be used any more, plus it would be convenient to invent a new one to avoid migration mistakes.<br class=""><br class="">Examples I know of this feature in other languages and/or implementations which can be used for reference/inspiration:<br class=""><br class="">- C++. I asked <a href="http://stackoverflow.com/questions/23726038/how-can-i-create-a-new-primitive-type-using-c11-style-strong-typedefs" class="">http://stackoverflow.com/questions/23726038/how-can-i-create-a-new-primitive-type-using-c11-style-strong-typedefs</a> while following a talk by Bjarne Stroustrup and some of the answers include using boost units (<a href="http://www.boost.org/doc/libs/1_60_0/doc/html/boost_units/Units.html" class="">http://www.boost.org/doc/libs/1_60_0/doc/html/boost_units/Units.html</a>) to alleviate writing the required boilerplate code. The library was already mentioned here as reference in previous threads.<br class=""><br class="">- Nim (<a href="http://nim-lang.org" class="">http://nim-lang.org</a>). Defined as distinct types (<a href="http://nim-lang.org/docs/manual.html#types-distinct-type" class="">http://nim-lang.org/docs/manual.html#types-distinct-type</a>) they can be used with the borrow pragma and templates to avoid boilerplate code yet customise which existing procs can be applied to the new distinct type.<br class=""><br class="">- Java. The language doesn’t allow this kind of fake types, but it has been patched to various degrees through annotation support. Recently Android Studio added resource type annotations (<a href="http://tools.android.com/tech-docs/support-annotations#TOC-Resource-Type-Annotations" class="">http://tools.android.com/tech-docs/support-annotations#TOC-Resource-Type-Annotations</a>) for mobile developers which allow marking integers with @StringRes, @IntegerRes and similar. The linter will look for these annotations and issue warnings when the assigned literals don’t satisfy the requirements. A more generic approach can be found in the Checker Framework in the Subtyping checker (<a href="http://types.cs.washington.edu/checker-framework/current/checker-framework-manual.html#subtyping-checker" class="">http://types.cs.washington.edu/checker-framework/current/checker-framework-manual.html#subtyping-checker</a>). The provided example shows how this hypothetical encrypted/plaintext string distinction would work. They also support the typical units annotations (<a href="http://types.cs.washington.edu/checker-framework/current/checker-framework-manual.html#units-checker" class="">http://types.cs.washington.edu/checker-framework/current/checker-framework-manual.html#units-checker</a>).<br class=""><br class="">Precisely this last reference is an argument against implementing this feature directly at the language level. I’ve seen that there is an attributes proposal (<a href="https://github.com/rsmogura/swift/wiki/Attributes-Proposal" class="">https://github.com/rsmogura/swift/wiki/Attributes-Proposal</a>) which would provide the equivalent of java’s annotations. Given that Java has a checker framework it would surely make sense to implement this similarly in swift? I don’t think so. Essentially nobody I’ve worked with knows about such annotations or external tools (sad). While anecdotal, it’s easy for me to think that annotations are easy to brush off and forget.<br class=""><br class="">And that’s why I’ve written this email, hopefully I can spur interest in others for this feature so that it becomes a first class citizen of the language at some point. I still haven’t got enough experience with swift to make a more concrete proposal. If nobody picks this up you may hear from me in the future.<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></div></div></div></body></html>