|
| 1 | +package hylo |
| 2 | + |
| 3 | +/** A wrapper around an object providing a reference API. */ |
| 4 | +private final class Ref[T](val value: T) { |
| 5 | + |
| 6 | + override def toString: String = |
| 7 | + s"Ref($value)" |
| 8 | + |
| 9 | +} |
| 10 | + |
| 11 | +/** A type-erased value. |
| 12 | + * |
| 13 | + * An `AnyValue` forwards its operations to a wrapped value, hiding its implementation. |
| 14 | + */ |
| 15 | +final class AnyValue private ( |
| 16 | + private val wrapped: AnyRef, |
| 17 | + private val _copy: (AnyRef) => AnyValue, |
| 18 | + private val _eq: (AnyRef, AnyRef) => Boolean, |
| 19 | + private val _hashInto: (AnyRef, Hasher) => Hasher |
| 20 | +) { |
| 21 | + |
| 22 | + /** Returns a copy of `this`. */ |
| 23 | + def copy(): AnyValue = |
| 24 | + _copy(this.wrapped) |
| 25 | + |
| 26 | + /** Returns `true` iff `this` and `other` have an equivalent value. */ |
| 27 | + def eq(other: AnyValue): Boolean = |
| 28 | + _eq(this.wrapped, other.wrapped) |
| 29 | + |
| 30 | + /** Hashes the salient parts of `this` into `hasher`. */ |
| 31 | + def hashInto(hasher: Hasher): Hasher = |
| 32 | + _hashInto(this.wrapped, hasher) |
| 33 | + |
| 34 | + /** Returns the value wrapped in `this` as an instance of `T`. */ |
| 35 | + def unsafelyUnwrappedAs[T]: T = |
| 36 | + wrapped.asInstanceOf[Ref[T]].value |
| 37 | + |
| 38 | + /** Returns a textual description of `this`. */ |
| 39 | + override def toString: String = |
| 40 | + wrapped.toString |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +object AnyValue { |
| 45 | + |
| 46 | + /** Creates an instance wrapping `wrapped`. */ |
| 47 | + def apply[T: Value](wrapped: T): AnyValue = |
| 48 | + def copy(a: AnyRef): AnyValue = |
| 49 | + AnyValue(a.asInstanceOf[Ref[T]].value.copy()) |
| 50 | + |
| 51 | + def eq(a: AnyRef, b: AnyRef): Boolean = |
| 52 | + a.asInstanceOf[Ref[T]].value `eq` b.asInstanceOf[Ref[T]].value |
| 53 | + |
| 54 | + def hashInto(a: AnyRef, hasher: Hasher): Hasher = |
| 55 | + a.asInstanceOf[Ref[T]].value.hashInto(hasher) |
| 56 | + |
| 57 | + new AnyValue(Ref(wrapped), copy, eq, hashInto) |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +given AnyValue is Value: |
| 62 | + |
| 63 | + extension (self: AnyValue) |
| 64 | + def copy(): AnyValue = self.copy() |
| 65 | + def eq(other: AnyValue): Boolean = self `eq` other |
| 66 | + def hashInto(hasher: Hasher): Hasher = self.hashInto(hasher) |
| 67 | + |
0 commit comments