# Improve opaque types

**URL:** <https://contributors.scala-lang.org/t/improve-opaque-types/4786>\
**Category:** Language Design\
**Created:** [January 5, 2021, 12:59pm UTC](https://contributors.scala-lang.org/t/improve-opaque-types/4786 "2021-01-05T12:59:16Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![Jasper-M](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jasper-m/32/1192_2.png) [@Jasper-M](https://contributors.scala-lang.org/u/Jasper-M)\
**Post date:** [January 5, 2021, 4:26pm UTC](https://contributors.scala-lang.org/t/improve-opaque-types/4786/12 "2021-01-05T16:26:54Z")

</div>

You can abstract this stuff away pretty well:

```scala
scala> trait NewType[A]:
     | opaque type Type = A
     | def apply(a: A): Type = a
     | extension (a: Type) def value: A = a
     | end NewType
// defined trait NewType

scala> object Bar extends NewType[String]
// defined object Bar

scala> val bar = Bar("foo")                                                
val bar: Bar.Type = foo

scala> bar.value
val res6: String = foo

```

Of course it’s not 100% the same API and has some drawbacks as discussed in the other thread (mainly with boxing of primitives).

---

_[View the full topic](https://contributors.scala-lang.org/t/improve-opaque-types/4786)._
