Skip to content

Instantly share code, notes, and snippets.

@adriaanm
Created September 2, 2016 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adriaanm/fc000f45f84226b9447f063c4d6a1331 to your computer and use it in GitHub Desktop.
Save adriaanm/fc000f45f84226b9447f063c4d6a1331 to your computer and use it in GitHub Desktop.
package scala.runtime
/* Classes used as holders for local lazy vals */
class LazyRef[T] {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized
private[this] var _value: T = _
def value: T = value
def initialize(value: T): T = {
_value = value
_initialized = true
value
}
override def toString = s"LazyRef${if (_initialized) s", computed: ${_value.toString}" else " thunk" }"
}
class LazyBoolean {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized
private[this] var _value: Boolean = _
def value: Boolean = value
def initialize(value: Boolean): Boolean = {
_value = value
_initialized = true
value
}
override def toString = s"LazyBoolean${if (_initialized) s", computed: ${_value.toString}" else " thunk" }"
}
class LazyByte {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized
private[this] var _value: Byte = _
def value: Byte = value
def initialize(value: Byte): Byte = {
_value = value
_initialized = true
value
}
override def toString = s"LazyByte${if (_initialized) s", computed: ${_value.toString}" else " thunk" }"
}
class LazyChar {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized
private[this] var _value: Char = _
def value: Char = value
def initialize(value: Char): Char = {
_value = value
_initialized = true
value
}
override def toString = s"LazyChar${if (_initialized) s", computed: ${_value.toString}" else " thunk" }"
}
class LazyShort {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized
private[this] var _value: Short = _
def value: Short = value
def initialize(value: Short): Short = {
_value = value
_initialized = true
value
}
override def toString = s"LazyShort${if (_initialized) s", computed: ${_value.toString}" else " thunk" }"
}
class LazyInt {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized
private[this] var _value: Int = _
def value: Int = value
def initialize(value: Int): Int = {
_value = value
_initialized = true
value
}
override def toString = s"LazyInt${if (_initialized) s", computed: ${_value.toString}" else " thunk" }"
}
class LazyLong {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized
private[this] var _value: Long = _
def value: Long = value
def initialize(value: Long): Long = {
_value = value
_initialized = true
value
}
override def toString = s"LazyLong${if (_initialized) s", computed: ${_value.toString}" else " thunk" }"
}
class LazyFloat {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized
private[this] var _value: Float = _
def value: Float = value
def initialize(value: Float): Float = {
_value = value
_initialized = true
value
}
override def toString = s"LazyFloat${if (_initialized) s", computed: ${_value.toString}" else " thunk" }"
}
class LazyDouble {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized
private[this] var _value: Double = _
def value: Double = value
def initialize(value: Double): Double = {
_value = value
_initialized = true
value
}
override def toString = s"LazyDouble${if (_initialized) s", computed: ${_value.toString}" else " thunk" }"
}
class LazyUnit {
@volatile private[this] var _initialized: Boolean = _
def initialized = _initialized
def initialize(): Unit = _initialized = true
override def toString = s"LazyUnit${if (_initialized) s", computed" else {"thunk"} }"
}
@drewhk
Copy link

drewhk commented Sep 2, 2016

Why classes? Can't the compiler simply generate the code in place to avoid pointer chasing and class header overhead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment