1 package frost.core
2
3 ====================================================================================================
4 Represents objects which may be passed by value instead of by reference. Value objects may not be
5 declared `@extendable`, may not be compared using the `==` / `!==` operators, and as `Value` is a
6 subclass of [Immutable], are naturally immutable as well. "Tricking" Frost into using the `==` /
7 `!==` operators on `Value`s, such as in:
8
9 def i1:Object := 5 -- Int is a subclass of Value
10 def i2:Object := 5
11 Console.printLine(i1 == i2)
12
13 gives undefined results. This could legally print either `true` or `false`, and the result may
14 differ from run to run of the same code.
15
16 #Performance
17
18 These restrictions permit the compiler to make several important optimizations around `Value`
19 objects. These are implementation details - the Frost compiler currently treats `Value`s as
20 described below, but is not *required* to and it would be valid for it to treat `Value`s just as any
21 other object.
22
23 Values are not allocated on the heap and do not need to be reference counted. Because they cannot be
24 subclassed, they do not need to store a pointer to their class the way most objects do. A `Value`
25 object is therefore just a simple structure consisting of its fields, exactly like a `struct` in the
26 C programming language, with the same performance characteristics.
27
28 Converting a `Value` to an ordinary class type (e.g. `def o:Object := 5`) requires it to be
29 "wrapped" in an actual `Object` instance. This wrapping is handled automatically by the compiler,
30 but does carry a performance penalty due to the memory allocation and reference counting it entails.
31
32 Converting a `Value` to its corresponding nullable type (e.g. `def i:Int? := 5`) is much cheaper
33 than wrapping it. A nullable `Value` is internally represented using only a single extra [Bit] to
34 distinguish `null` from non-`null` values. Nullable `Value`s are still stack allocated and passed by
35 value with no reference counting.
36
37 Again, the compiler handles all of these details automatically. It is only explained here so that
38 the performance implications are clear.
39 ====================================================================================================
40 @abstract
41 class Value : Immutable {
42 }