Skip to content

Commit

Permalink
LoxInstance doesnt need a UUID; we can simply use the built in `Obj…
Browse files Browse the repository at this point in the history
…ectIdentifier` for instances for hashing, and compare references for equality.
  • Loading branch information
quephird committed Apr 1, 2024
1 parent c038907 commit 1577cd6
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
4 changes: 0 additions & 4 deletions slox/LoxInstance.swift
Expand Up @@ -5,8 +5,6 @@
// Created by Danielle Kefford on 3/4/24.
//

import Foundation

class LoxInstance {
// `klass` is what is used in the interpreter when we need
// to know the class of a particular instance. Every Lox
Expand All @@ -26,14 +24,12 @@ class LoxInstance {
return _klass!
}
var properties: [String: LoxValue] = [:]
var objectId: UUID

/// - Parameter klass: The class this instance belongs to.
/// Use `nil` if this instance *is* a class; the `klass` property
/// will then instantiate a metaclass for it on demand.
required init(klass: LoxClass?) {
self._klass = klass
self.objectId = UUID()
}

func get(propertyName: String) throws -> LoxValue {
Expand Down
4 changes: 2 additions & 2 deletions slox/LoxValue.swift
Expand Up @@ -144,7 +144,7 @@ enum LoxValue: CustomStringConvertible, Equatable, Hashable {
case (.instance(let leftDict as LoxDictionary), .instance(let rightDict as LoxDictionary)):
return leftDict.kvPairs == rightDict.kvPairs
case (.instance(let leftInstance), .instance(let rightInstance)):
return leftInstance.objectId == rightInstance.objectId
return leftInstance === rightInstance
default:
return false
}
Expand All @@ -170,7 +170,7 @@ enum LoxValue: CustomStringConvertible, Equatable, Hashable {
case .nativeFunction(let nativeFunction):
hasher.combine(nativeFunction)
case .instance(let instance):
hasher.combine(instance.objectId)
hasher.combine(ObjectIdentifier(instance))
}
}
}

0 comments on commit 1577cd6

Please sign in to comment.