Skip to content

Commit f442519

Browse files
committed
Define almost all Predef overwrites directly in Scala 2 library TASTy
The `valueOf` is not implemented yet.
1 parent 27f1a13 commit f442519

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

scala2-library-bootstrapped/src/scala/Predef.scala

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,61 @@ object Predef extends LowPriorityImplicits {
508508
*/
509509
// $ to avoid accidental shadowing (e.g. scala/bug#7788)
510510
implicit def $conforms[A]: A => A = <:<.refl
511+
512+
// TODO Add `valueOf`:
513+
// Issue is that this introduces a cyclic dependency between
514+
// the Scala 2 and 3 library due to the reference to `summonFrom`.
515+
// /**
516+
// * Retrieve the single value of a type with a unique inhabitant.
517+
// *
518+
// * @example {{{
519+
// * object Foo
520+
// * val foo = valueOf[Foo.type]
521+
// * // foo is Foo.type = Foo
522+
// *
523+
// * val bar = valueOf[23]
524+
// * // bar is 23.type = 23
525+
// * }}}
526+
// * @group utilities
527+
// */
528+
// inline def valueOf[T]: T = summonFrom {
529+
// case ev: ValueOf[T] => ev.value
530+
// }
531+
532+
/** Summon a given value of type `T`. Usually, the argument is not passed explicitly.
533+
*
534+
* @tparam T the type of the value to be summoned
535+
* @return the given value typed: the provided type parameter
536+
*/
537+
transparent inline def summon[T](using x: T): x.type = x
538+
539+
// Extension methods for working with explicit nulls
540+
541+
/** Strips away the nullability from a value. Note that `.nn` performs a checked cast,
542+
* so if invoked on a `null` value it will throw an `NullPointerException`.
543+
* @example {{{
544+
* val s1: String | Null = "hello"
545+
* val s2: String = s1.nn
546+
*
547+
* val s3: String | Null = null
548+
* val s4: String = s3.nn // throw NullPointerException
549+
* }}}
550+
*/
551+
extension [T](x: T | Null) inline def nn: x.type & T =
552+
if x.asInstanceOf[Any] == null then scala.runtime.Scala3RunTime.nnFail()
553+
x.asInstanceOf[x.type & T]
554+
555+
extension (inline x: AnyRef | Null)
556+
/** Enables an expression of type `T|Null`, where `T` is a subtype of `AnyRef`, to be checked for `null`
557+
* using `eq` rather than only `==`. This is needed because `Null` no longer has
558+
* `eq` or `ne` methods, only `==` and `!=` inherited from `Any`. */
559+
inline def eq(inline y: AnyRef | Null): Boolean =
560+
x.asInstanceOf[AnyRef] eq y.asInstanceOf[AnyRef]
561+
/** Enables an expression of type `T|Null`, where `T` is a subtype of `AnyRef`, to be checked for `null`
562+
* using `ne` rather than only `!=`. This is needed because `Null` no longer has
563+
* `eq` or `ne` methods, only `==` and `!=` inherited from `Any`. */
564+
inline def ne(inline y: AnyRef | Null): Boolean =
565+
!(x eq y)
511566
}
512567

513568
/** The `LowPriorityImplicits` class provides implicit values that

0 commit comments

Comments
 (0)