<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br><br>Sent from my iPad</div><div><br>On Dec 17, 2015, at 3:33 PM, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><span></span><br><blockquote type="cite"><span>On Dec 17, 2015, at 4:23 AM, Daniel Steinberg via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>The way in which we reason about instances of value types and instances of reference types is significantly different and yet there is no way to easily distinguish them in Swift.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>In Objective-C, for example, if we have an NSDate and an NSTimeInterval we can tell that NSDate is a reference type and NSTimeInterval is a value type by looking at how it is used in code</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>NSDate *myDate = //…</span><br></blockquote><blockquote type="cite"><span>NSTimeInterval someTimeInterval = //…</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>The “*” helps us see that myDate is a pointer to an instance of NSDate.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>We see this in methods that return values as well</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>- (NSString *)aStrringReturningMethod // …</span><br></blockquote><blockquote type="cite"><span>- (NSInteger)numberOfMistakesInThisEmail //...</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>I realize that this is a result of Objective-C living in C’s world and we don’t have that constraint in Swift.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>However, this means that when the semantics of variables, parameters, properties, and return values from methods is not always clear in our code. Because structs can have methods in Swift it is all to easy to confuse an instance of a struct with an instance of a class.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>I may be alone here, but I think it would be less confusing if there were some way to distinguish between value types and reference types in code.</span><br></blockquote><span></span><br><span>The real differences happen whenever you mutate them by part (assignment acts the same), which suggests that they should have different syntax for member access, e.g. someObject->mutatingMethod() vs. someValue.mutatingMethod().</span><br></div></blockquote><div><br></div><div>Of course real differences also exist in the possibility that someone else could mutate a <b>shared</b> instance of a reference type behind your back. Representing this difference syntactically requires a type annotation, not just a different syntax for member access. It's also worth noting that value types and <b>non-shared </b>instances of reference types actually have pretty similar semantics.</div><div><br></div><div>I would like to think there is a good way to clearly represent these differences in a language as they are quite important. The reality so far has been that attempting to do so introduces quite a bit of complexity. Rust is a current example of this.</div><div><br></div><div>My understanding is that an important goal for Swift is to avoid this kind of complexity in the type system and in the syntax. As noted in this thread doing so doesn't make it go away, it just sweeps it under the rug a bit.</div><div><br></div><div>If there were a way to surface the important semantic distinctions without making the language terribly complex and without sacrificing semantically valid generic code it would probably be embraced. I think this is just a really hard problem for which we don't yet have a great solution. </div><br><blockquote type="cite"><div><span></span><br><span>Personally, I like that sort of thing. Not to discourage you, but I think most people don’t recognize that types with value semantics are fundamentally different from those with reference semantics, e.g. avoiding a syntactic barrier between them was an explicit design goal of Swift 1. Also I don’t think you’d find much support for even a 1-character penalty on method calls and property accesses to classes. So, while I think we ought to do something in this direction, I expect it to be hard-won.</span><br><span></span><br><span>-Dave</span><br><span></span><br><span></span><br><span></span><br><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>