---
# System prepended metadata

title: Bob 筆記

---

# Bob 筆記





## Value v.s. Reference

- https://en.wikipedia.org/wiki/Value_type_and_reference_type
- https://mangrove-roadway-577.notion.site/JS-pass-by-sharing-2f37c177c08043afa13d6dd1d68b7841

#### javascript
```javascript=
// like call by reference
let obj1 = {number: 3}
let obj2 = obj1
obj1.number = 10
console.log(obj2) // {number: 10}

// like call by value
obj1 = {number: 7}
console.log(obj2) // {number: 10}
```

#### Swift

```swift=
// call by reference
class Person {
    var name: String
}
var man1 = Person(name: "Bob")
var man2 = man1
man1.name = "Leo"
print(man1.name) // Leo
print(man2.name) // Leo
```

```swift=
// call by value
struct Ticket {
    var price: Int
}
var ticket1 = Ticket(price: 10)
var ticket2 = ticket1
ticket1.price = 7
print(ticket1.price) // 7
print(ticket2.price) // 10
```



## [Kotlin: `Sealed Classes`](https://louis383.medium.com/kotlin-sealed-classes-%E7%9A%84%E5%9F%BA%E7%A4%8E%E4%BD%BF%E7%94%A8-de660dbb63d2)

在表達一個元件的狀態時，可能第一個想到的會是 `enum class` 。而現在有了新的選擇—— `sealed class` 。它們倆的差別是： `enum class` 可以拿來簡單的判斷狀態，但不能傳遞變數；反之，`sealed class` ==可以攜帶變數==。

> Kotlin 的 `Sealed Classes` 看起來像是 Swift 的 [Associated Values](https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html#ID148)。 

> 與 Swift 不同，Kotlin 的 `enum class` 或 `sealed class` 的本身應該都是物件(class)。所以才會需要轉型（搭配 Kotlin 的 when 幫你做掉）

`enum class` 範例：
```kotlin=
enum class MachineState {
    START, SLEEP, RUNNING
}
```

`sealed class` 範例：
```kotlin=
sealed class WorkingState {
    // data class
    data class Finished(val result: List<String>): WorkingState()
    data class ErrorHappened(val whatHappened: String): WorkingState()
    // object
    object Working: WorkingState()
    object EmptyResult: WorkingState()
}
```