# Pre-release: Shallow and Optional Types

**URL:** <https://racket.discourse.group/t/pre-release-shallow-and-optional-types/1303>\
**Category:** Announcements\
**Created:** [September 15, 2022, 6:44pm UTC](https://racket.discourse.group/t/pre-release-shallow-and-optional-types/1303 "2022-09-15T18:44:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben\_greenman](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/ben_greenman/32/632_2.png) [@ben\_greenman](https://racket.discourse.group/u/ben_greenman)\
**Post date:** [September 15, 2022, 6:44pm UTC](https://racket.discourse.group/t/pre-release-shallow-and-optional-types/1303/1 "2022-09-15T18:44:19Z")

</div>

Typed Racket comes with two faster-but-weaker alternatives to its normal "Deep" types in the latest snapshot builds:

[https://snapshot.racket-lang.org/](https://snapshot.racket-lang.org/)

Give them a try & report any issues!

"_Faster but weaker_" refers to the way these languages enforce types at the boundaries to untyped code:

- Deep TR (`#lang typed/racket`) enforces types with contracts, which may add a huge run-time cost but always protect against miscommunications.
- Shallow TR (`#lang typed/racket/shallow`) enforces types with shape checks, which run quickly but detect only big mistakes --- e.g., expected a list, got a string. Very useful for typed scripts that interact with untyped libraries.
- Optional TR (`#lang typed/racket/optional`) enforces types with ... nothing! Types are checked statically and are not enforced at run-time.

All three use the same static typechecker.

Further reading:

- [Typed Racket Guide (snapshot)](https://www.cs.utah.edu/plt/snapshots/current/doc/ts-guide/typed-untyped-interaction.html#%28part._protecting-interaction%29%3E)
- [Typed Racket Reference (snapshot)](https://www.cs.utah.edu/plt/snapshots/current/doc/ts-reference/behavior-of-types.html)
- [Racket Blog announcement (draft)](https://github.com/racket/racket-lang-org/pull/186/files)

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [September 15, 2022, 9:02pm UTC](https://racket.discourse.group/t/pre-release-shallow-and-optional-types/1303/2 "2022-09-15T21:02:18Z")

</div>

Very cool! I'm guessing that neither of these two new modes affect compilation times much?

---

<div class="post-metadata">

**Author:** ![ben\_greenman](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/ben_greenman/32/632_2.png) [@ben\_greenman](https://racket.discourse.group/u/ben_greenman)\
**Post date:** [September 15, 2022, 9:10pm UTC](https://racket.discourse.group/t/pre-release-shallow-and-optional-types/1303/3 "2022-09-15T21:10:53Z")

</div>

Nope, not much. Type checking is as fast or slow as ever.

There is some change after typechecking because there are 3 paths now (generate contracts vs shape checks etc), but I haven’t noticed big differences.

---

<div class="post-metadata">

**Author:** ![rscho](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/rscho/32/726_2.png) [@rscho](https://racket.discourse.group/u/rscho)\
**Post date:** [September 15, 2022, 9:55pm UTC](https://racket.discourse.group/t/pre-release-shallow-and-optional-types/1303/4 "2022-09-15T21:55:33Z")

</div>

Congratulations!

Does this mean that libs heavily using `typed/racket`, such as `math/array` might become usable from plain racket by just changing the `#lang` line at the top of every lib file? @soegaard, could we finally be removing the [50x performance penalty warning](https://docs.racket-lang.org/math/array.html) soon?

---

<div class="post-metadata">

**Author:** ![MichaelBurge](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/michaelburge/32/727_2.png) [@MichaelBurge](https://racket.discourse.group/u/MichaelBurge)\
**Post date:** [September 15, 2022, 11:26pm UTC](https://racket.discourse.group/t/pre-release-shallow-and-optional-types/1303/5 "2022-09-15T23:26:23Z")

</div>

> Optional types cannot detect incorrect type assumptions and therefore do not enable type-driven optimizations

Would it be possible to apply optimizations by assuming the type signatures are correct?

One use is for timing: By converting one "hot spot" module and adding only annotations for anything else, you would get full TR optimizations and no contract slowdown. So you could measure the "best-case" scenario when deciding whether to convert a whole project to TR.

The downside: Optimized Optional TR could cause a segfault by e.g. an `unsafe-vector-ref` being applied to a list that somebody claims is a vector. At least it's now easy to change to shallow/deep for debugging. But that might not be enough in a security context(Racket websites exposed to the general internet, or a Discord bot that evaluates arbitrary code), so maybe hide it behind an option?

---

<div class="post-metadata">

**Author:** ![ben\_greenman](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/ben_greenman/32/632_2.png) [@ben\_greenman](https://racket.discourse.group/u/ben_greenman)\
**Post date:** [September 16, 2022, 4:48am UTC](https://racket.discourse.group/t/pre-release-shallow-and-optional-types/1303/6 "2022-09-16T04:48:11Z")

</div>

> [@rscho](#):
>
> Does this mean that libs heavily using `typed/racket`, such as `math/array` might become usable from plain racket by just changing the `#lang` line at the top of every lib file? @soegaard, could we finally be removing the [50x performance penalty warning](https://docs.racket-lang.org/math/array.html) soon?

I hope so! Let's talk more at [`racket/math` #75](https://github.com/racket/math/issues/75)

> [@MichaelBurge](#):
>
> Would it be possible to apply optimizations by assuming the type signatures are correct?

For extreme speed, I'd go with `unsafe-provide` and `unsafe-require/typed`  
(in `#lang typed/racket`)

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [September 16, 2022, 7:52am UTC](https://racket.discourse.group/t/pre-release-shallow-and-optional-types/1303/7 "2022-09-16T07:52:24Z")

</div>

That would be great - let's hear what Ben thinks.
