# [lworld] Add support for denoting and deriving the reference projection

XMLWordPrintable

#### Details

• Bug
• Status: Resolved
• P4
• Resolution: Fixed
• repo-valhalla
• generic
• generic

#### Description

From http://cr.openjdk.java.net/~briangoetz/valhalla/sov/02-object-model.html

Reference and value projections
It is often useful to be able to describe the set of references to objects of a given inline class, plus null. Given an inline type V, we would like a reference type R whose value set is given by:

ValSet(R) = {null} ∪ {ref v : v ∈ ValSet(V)}

We call such a type R a reference projection of V. (A reference type is its own reference projection.)

A reference projection plays the role that a wrapper class plays in the current world. But, we don’t want every inline class to have a hand-written, ad-hoc wrapper; we would like to be able to mechanically derive a reference projection from an inline class, and have a uniform way to refer to it – that way we don’t have to maintain a mental dictionary mapping inline classes to their reference projections. For any type T, T.ref denotes the reference projection of T.

For inline classes, we automatically create both the reference and value projections. For an inline class V, V.ref is a type that describes the reference projection for V (the set of references to instances of V, plus null), V.val refers to the value projection for V (the set of instances of V), and (absent special pleading, see below) V is an alias for V.val. (The reference projection is a sealed abstract class that permits only V.val as a subtype.) So for an inline class V that extends abstract class C, we effectively get:

sealed abstract class V.ref
extends C
permits V.val { }

inline class V.val extends V.ref { }
and V becomes an alias for V.val. (This sort of aliasing is not new; String is an alias for java.lang.String.) We automatically get an inline widening conversion from V.val to V.ref (and hence from V to V.ref). Additionally, we define an inline narrowing conversion from V.ref to V.val (and hence from V.ref to V) which applies the unref operator, throwing NullPointerException on null.

With this pair of conversions, inline classes have the same relationship with their reference projections as primitives historically did with their wrapper types. Existing rules that are defined in terms of boxing conversions (autoboxing, typing of conditionals, overload selection) can be trivially extended to incorporate inline widening and narrowing conversions too. The result is that existing language rules and user intuition about these conversions carries forward unchanged in the new world – but without the runtime costs of boxing, because inline widening does not mandate the creation of a new object with accidental identity as boxing currently does.

A reference type R already meets all the requirements to be its own reference projection, so for a reference type R, we make R.ref an alias for R itself. Now we’ve ensured that every type T has a reference projection that is denoted T.ref.

// ------------------------------------------------------------------------

This ticket is to add support for denoting, deriving and representing the reference projection type.