Stream: beginners

Topic: Dead code warning


view this post on Zulip Kiryl Dziamura (Jan 18 2024 at 21:09):

I found this code doesn't produce any warnings though is clearly dead. It’s probably related to the type system. AFAIU it should be relatively easy to construct a derivation chain at least when there’s no branching (but I guess it might affect compiling performance?). In the example, the closure (it can be an external function) doesn't use the first argument so it should be predictable that the preceding code in the pipeline does nothing

x =
    1
    |> \_ -> 3

view this post on Zulip Luke Boswell (Jan 18 2024 at 21:36):

Im not sure I understand why it should produce a warning? You are intentionally throwing away the value by using _.

view this post on Zulip Brendan Hansknecht (Jan 18 2024 at 21:40):

if x isn't used elsewhere, x should have a warning.

Otherwise, for the lambda, idk. Since it is an inline lambda, a warning is probably reasonable, but in general \_ is fine, it is explictly ignoring an arg.

view this post on Zulip Brendan Hansknecht (Jan 18 2024 at 21:43):

For example,

createExpensiveThing : {} -> ExpensiveThing
createExpensiveThing = \_ -> ... # arg not used, but needed to make this lazy

# or
fnMatchingSomeApi : U8, U8, I32 -> I32
fnMatchingSomeApi = \a, b, _ -> ... # last arg not used but needed to match api.

view this post on Zulip Kiryl Dziamura (Jan 18 2024 at 21:54):

To rephrase my example:

ignoreEverythingAbove = \_, x-> x

y =
  1
  |> expensiveComputation
  |> ignoreEverythingAbove 42

Since there are no side effects - both 1 and expensiveComputation call are dead.
Can’t say if it’s a real world problem tho.

view this post on Zulip Brendan Hansknecht (Jan 18 2024 at 22:51):

Yeah, that's probably reasonable, but we have to be smart about it cause:

  1. an arg may exist but not be used because of implementation changes, but the same api may be wanted.
  2. {} is often an unused empty arg.

Last updated: Jul 05 2025 at 12:14 UTC