# Dependent typing on lazy argument (Scala 3)

**URL:** <https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562>\
**Category:** Question\
**Created:** [June 28, 2021, 8:43pm UTC](https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562 "2021-06-28T20:43:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![poslegm](https://users.scala-lang.org/user_avatar/users.scala-lang.org/poslegm/32/1847_2.png) [@poslegm](https://users.scala-lang.org/u/poslegm)\
**Post date:** [June 28, 2021, 8:43pm UTC](https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562/1 "2021-06-28T20:43:29Z")

</div>

Hello!

I tried Match Types for dependent typing in function with lazy argument. And its value was computed at runtime. Is this the expected behavior?

Scastie example: [Scastie - An interactive playground for Scala](https://scastie.scala-lang.org/X1v0cUIoRq2Zv95tKBEFWw)

I thought that type checking is done at compile time. Is there a way to do dependent typing with the preservation of argument laziness in Scala 3?

---

<div class="post-metadata">

**Author:** ![crater2150](https://users.scala-lang.org/user_avatar/users.scala-lang.org/crater2150/32/1011_2.png) [@crater2150](https://users.scala-lang.org/u/crater2150)\
**Post date:** [June 28, 2021, 9:11pm UTC](https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562/2 "2021-06-28T21:11:22Z")

</div>

I’m not sure what exactly you’re trying to do, but the reason `test` runs its block and `plain` doesn’t, has nothing to do with its return type.

In `plain`, you just return a string and don’t use `t`. But in `test`:

```auto
def test[T](t: => T): Construct[T] =
  t match
    case _: Any => "not used"

```

you match on `t`, which causes it to be evaluated. A by-name parameter is evaluated when it is used, and matching on it uses it, even when the only case ignores its value.

---

<div class="post-metadata">

**Author:** ![poslegm](https://users.scala-lang.org/user_avatar/users.scala-lang.org/poslegm/32/1847_2.png) [@poslegm](https://users.scala-lang.org/u/poslegm)\
**Post date:** [June 28, 2021, 10:50pm UTC](https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562/3 "2021-06-28T22:50:05Z")

</div>

I’m trying to compute return type of function `test` without its argument evaluation.

A more meaningful example: [Scastie - An interactive playground for Scala](https://scastie.scala-lang.org/QmCmjJHdSIyQ6a1xxvySUQ)

I hoped that match expression for match types works at compile-time only and doesn’t affect variables at runtime. I know that it can be done with [dependent methods and implicit machinery](https://github.com/zio/zio/issues/5241), but match types looks like a more elegant solution.

And I don’t understand why value of argument is computed if match clause checks only type known at compile time.

---

<div class="post-metadata">

**Author:** ![BalmungSan](https://users.scala-lang.org/user_avatar/users.scala-lang.org/balmungsan/32/1700_2.png) [@BalmungSan](https://users.scala-lang.org/u/BalmungSan)\
**Post date:** [June 28, 2021, 10:55pm UTC](https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562/4 "2021-06-28T22:55:27Z")

</div>

A `match` is a runtime expression, it is evaluated at runtime.

In your case how would you expect it to pick the correct branch to execute? At ru time due type erasure `Input` is nothing more than `Object`

---

<div class="post-metadata">

**Author:** ![poslegm](https://users.scala-lang.org/user_avatar/users.scala-lang.org/poslegm/32/1847_2.png) [@poslegm](https://users.scala-lang.org/u/poslegm)\
**Post date:** [June 28, 2021, 11:08pm UTC](https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562/5 "2021-06-28T23:08:01Z")

</div>

Ok, looks like I incorrectly understand [documentation](https://dotty.epfl.ch/docs/reference/new-types/match-types.html#dependent-typing) about “special mode of typing for match expressions”. Thank you for replies.

---

<div class="post-metadata">

**Author:** ![BalmungSan](https://users.scala-lang.org/user_avatar/users.scala-lang.org/balmungsan/32/1700_2.png) [@BalmungSan](https://users.scala-lang.org/u/BalmungSan)\
**Post date:** [June 28, 2021, 11:14pm UTC](https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562/6 "2021-06-28T23:14:59Z")

</div>

Uhm, maybe with `inline` it would work?

I mean, at some point the compiler knows the correct type of the expression, so maybe it could online the `match`

Maybe even is just an oversight from the maintainers in reference to _by-name_ parameters.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [June 29, 2021, 8:12am UTC](https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562/7 "2021-06-29T08:12:57Z")

</div>

If you want a “compile time” match on the type you can do this:

```auto
import scala.compiletime.erasedValue

inline def test[T](t: => T): Construct[T] =
  inline erasedValue[T] match
    case _: Any => "not used"

```

---

<div class="post-metadata">

**Author:** ![poslegm](https://users.scala-lang.org/user_avatar/users.scala-lang.org/poslegm/32/1847_2.png) [@poslegm](https://users.scala-lang.org/u/poslegm)\
**Post date:** [June 29, 2021, 7:44pm UTC](https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562/8 "2021-06-29T19:44:53Z")

</div>

I solved it with `summonFrom` and `transparent inline`: [Scastie - An interactive playground for Scala](https://scastie.scala-lang.org/EwfBu7rcTwi6UQ11VYMwEQ)

Looks verbose but still better than Scala 2 🙂

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [June 29, 2021, 10:54pm UTC](https://users.scala-lang.org/t/dependent-typing-on-lazy-argument-scala-3/7562/9 "2021-06-29T22:54:33Z")

</div>

That actually looks like a great use case for an inline match with `erasedValue`. At least if that match type works for you. And even without the match type you could probably use `transparent`.
