[swift-users] Comparing POP to OOP

Jon Hoffman hoffman.jon at gmail.com
Mon Mar 7 20:38:08 CST 2016


> On Mar 7, 2016, at 6:15 PM, Dave Abrahams <dabrahams at apple.com> wrote:
> 
> 
> on Mon Mar 07 2016, Jon Hoffman <hoffman.jon-AT-gmail.com <http://hoffman.jon-at-gmail.com/>> wrote:
> 
>>> On Mar 6, 2016, at 11:53 PM, Dave Abrahams <dabrahams at apple.com> wrote:
>>> 
>>> 
>>> on Sun Mar 06 2016, Jon Hoffman <hoffman.jon-AT-gmail.com> wrote:
>>> 
>> 
>>>>> 
>>> 
>>> “Protocol-oriented” doesn't mean “no reference types.”  It means you're
>>> not forced into reference semantics just because you want polymorphism.
>>> 
>>>> For example we cannot create a linked list with a value type.
>>> 
>>> Not true, in fact.
>>> 
>>> enum List<T> {
>>>    case Empty
>>>    indirect case NonEmpty(T, List)
>>> } 
>>> 
>>> var x: List = .NonEmpty(3, .NonEmpty(4, .Empty))
>>> 
>>> There are other ways, but that's the simplest in Swift.
>> 
>> I stand corrected; I neglected to think about using the enumeration.
> 
> Note again: there are other ways, e.g. you can make it immutable or
> build it with COW.

So for the last two hours or so I put myself on a task to create a mutable linked list using value types and to do it in an elegant way (Hey I love challenges and beating my head against the wall).  While I have found one working solution so far it is pretty ugly therefore I have to ask myself, that even though it is preferred that we use value types overall, should it be preferred that we use reference types for mutable data structures like this?  Trust me, I have not giving up finding an elegant solution yet just wondering your thoughts on that.



> 
>> Yet another blog post coming from our conversation.
>> 
>>> 
>>>> If we tried we would get a compile time error that says: “Recursive
>>>> value type ‘Type’ is not allowed.
>>>> 
>>>> There are two arguments that I could make off the top of my head
>>>> against my last statement.  The first argument would be: Is preferring
>>>> value types to reference types actually part of OOP.  
>>> 
>>> I presume you meant POP.  Being forced to use reference types is part of
>>> OOP.
>>> 
>>>> I will address this in my next e-mail where you ask me what I mean by
>>>> my statement that POP is about so much more than the protocol.  The
>>>> second argument would be that we are not limited to value types with
>>>> POP and we are able to still use reference types.
>>> 
>>> Exactly.
>>> 
>>>> While it is true that we are still able to use value types 
>>>> with POP
>>> 
>>> reference types?
>> 
>> Yes sometimes the fingers don’t always type what the brain tells them too.  :) 
>> 
>>> 
>>>> however if we take Apple’s recommendation and use value types
>>>> for our custom types we really should try our best to avoid reference
>>>> types.  It could get confusing if some of our types are reference
>>>> types and some are value types especially for larger enterprise type
>>>> projects.  
>>> 
>>> No, some things are fundamentally references, as I noted in the talk:
>>> https://github.com/ASCIIwwdc/wwdc-session-transcripts/blob/master/2015/408.vtt#L3082
>>> 
>>>> Luckily in XCode with have the quick documentation where we can hold
>>>> down the option key and click on the type in question to tell us if it
>>>> is a value or reference type however we do not have that option
>>>> outside of the Apple’s ecosystem like when we are developing Linux
>>>> applications with Swift.  Therefore we need to be careful on how and
>>>> when to use reference and value types.
>>> 
>>> You always do need to be careful about that, regardless, because they
>>> have different semantics.
>> 
>> You are correct however my point with this is with other languages
>> like Java and C# our custom types are reference types. 
> 
> I don't think so:
> 
> C#: http://www.albahari.com/valuevsreftypes.aspx <http://www.albahari.com/valuevsreftypes.aspx>
> Coming in Java 9: http://www.theserverside.com/news/2240234686/Devoxx-officially-kicks-off-with-Spring-Java-EE-8-and-the-women-of-IT <http://www.theserverside.com/news/2240234686/Devoxx-officially-kicks-off-with-Spring-Java-EE-8-and-the-women-of-IT>
> 


About a year ago, at my day job we had to build an app for a Windows 8 tablet and I took on the challenge myself (love learning new things) and after using C# quite a bit over the last year I never realized that I could use structs in C# like I do in Swift.  Pretty cool, thanks. 

Jon


> -- 
> -Dave

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160307/4e6873a4/attachment.html>


More information about the swift-users mailing list