# Introducing Swift in higher education

**URL:** <https://forums.swift.org/t/introducing-swift-in-higher-education/53445>\
**Category:** Community Showcase\
**Created:** [November 12, 2021, 1:39pm UTC](https://forums.swift.org/t/introducing-swift-in-higher-education/53445 "2021-11-12T13:39:04Z")\
**Posts on this page:** 1\
**Showing post:** 27

<div class="post-metadata">

**Author:** ![kennyc](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kennyc/32/9167_2.png) [@kennyc](https://forums.swift.org/u/kennyc)\
**Post date:** [November 18, 2021, 7:08am UTC](https://forums.swift.org/t/introducing-swift-in-higher-education/53445/27 "2021-11-18T07:08:55Z")

</div>

> [@dabrahams](#):
>
> Sorry, sometimes I forget how deeply reference-semantic thinking is ingrained in the culture…

Very much so for some of us... ;)

> [@dabrahams](#):
>
> When you store an instance of value-semantic `Node` , you're storing (a copy of) its value, just like when you store an `Int` , you're storing a copy of _its_ value. You're not surprised that if you store the `x` coordinate of a `Point` , you can't magically reach back into the `Point` and mutate it just by changing your stored `Int` , are you?

No, but coming from Objective-C if I have an integer or a point, then I view those as primitive types. But if my view had some richer object, like a `Person`, then I would expect to be able to edit those properties. If the `Person` was a class, which it would likely be in Objective-C, then every other view with a reference to that `Person` would see the changes.

_(Admittedly, that's a problem unto itself and something value types try and prevent.)_

> [@dabrahams](#):
>
> you've already left the world of value semantics as soon as you have a class with a mutable member. If you want to stay within value semantics, make your model a `struct` .

I agree. Trying to use value types from top-to-bottom is the goal but that's where I'm hitting some walls. Inevitably, some classes start popping up and kind of break the implementation.

> [@dabrahams](#):
>
> Well, no, they each have _a copy_ of it, and thinking of them as references will get you in big trouble. But yes, if you store copies of arrays and then mutate them, you should expect to pay for it.

I'm playing a little loose with my terminology, but you're right. When I think of having a "reference" to something when discussing a type like a `Tree`, what I'm really thinking of is the reference to the `Array's` underlying shared storage.

So while I do have a copy, my copy does have a reference under the hood. This has tripped me up in the past when benchmarking some code and wondering why a seemingly simple mutation, like toggling a boolean property, was so slow. Turns out a copy I had made of a property was triggering CoW, but the performance degradation was only noticed when mutation occurred, now when the copy occurred. I completely understand why, but in my (very) early days of learning Swift it caught me off guard.

Point being, if I do have an extensive data structure that is getting mutated, it's important that nobody else have a copy of it if performance is important. That means checking if any views or intermediate components happen to be making a copy purely for convenience, or perhaps bad habits.

> [@dabrahams](#):
>
> Now, it may be that the Cocoa kits are really hostile to value semantics; they are, after all, based on an “everything is a reference” programming model.

In some ways, that's the answer I'm trying to determine. When looking at functional languages and functional frameworks, value semantics, or immutable data, make a lot of sense and it's relatively clear how to program within those constraints.

When dealing with pure Swift, I think that's also the case. But when using Swift within the context of an AppKit or UIKit application, where I'm sure the vast majority of Swift is used, then for me at least it gets a lot harder to reason about.

It might very well be that these older frameworks are not entirely compatible with pure value semantics, which is fine. But I've been curious if my difficulties are because of this or that I'm just "doing things wrong".

> [@dabrahams](#):
>
> Maybe you bring back the `Model` class and spread references to it around the views, but the views store keypaths into the model that describe the “address” of the specific node to which they correspond.

This is sort of the approach I've been playing with. A sort of "view model", which is a class, owns the data which is represent as a value type. How to "address" things and make the mutations is what started this thread for me.

> [@dabrahams](#):
>
> You have to expunge that from your thinking and your language if you want to deal effectively with value types directly. There is no "reference to a value." If `Tree` is a value type, there is no reference-to-a- `Tree` .

You're right. As noted above, what I was trying to say was that a copy of a `Tree` also has a copy of `rootNodes: Array`. Each copy of that `Array` does have a reference to the shared, underlying storage.

Thus, two views, each with their own copy of the `Tree`, indirectly also have a reference to the underlying storage. That's where I'm (incorrectly?) applying the word "reference".

– Appreciate all the input and lengthy replies.

---

_[View the full topic](https://forums.swift.org/t/introducing-swift-in-higher-education/53445)._
