OCaml warning 20: unused function argument

‘(Obj.magic f) a b’ triggers such warning saying a and b are not used. This warning happens rarely in practice, do you know a use case where such warning catches bugs? Thanks

You mean in the general case? For me it catches issues all the time, there are two usecases where this proves surprisingly valuable:

  1. I meant to do something with a function argument but didn’t. There was logic missing, but of course the type system couldn’t tell me that. Fortunately the warning told me that I forgot to do something with a value I passed it.
  2. My logic was wrong and I never needed the argument in the first place, that way I can clean up all my calls (and potentially remove the argument going all the way up the call stack) to avoid it.

Do you have a concrete piece of code that such warning helps? Thanks

Marek Kubica via OCaml ocaml@discoursemail.com于2020年3月31日 周二下午5:11写道:

The warning is useful for any function that always raises an exception or diverges?
For instance with failwith:

failwith "A string" ["anything"]


let loop state = 
  loop (update state)
let _ = loop state more
1 Like

Easily done:

let f x = failwith "foo" ;;
f 1 2;;

compiling yields:

5$ ocamlc foo.ml
File "foo.ml", line 4, characters 4-5:
4 | f 1 2;;
Warning 20: this argument will not be used by the function.

This has caught my errors several times.

Like from my production code? It’s too involved for a post since there is a lot of infrastructure code filled in.

But here’s a transliteration of case 1, the idea of code that we do run in production.

let lookup_username cache user_id =
  let%bind username = lookup_microservice user_id in
  return username

Here I forgot to look it up from cache or save it into the cache. The code compiles fine, it does not do the thing it was supposed to do. Now the compiler tells me, that I forgot to actually use cache. Very useful.

I first was annoyed that dune subscribes you to this warning by default and it can be somewhat annoying when debugging, but I’ve come to see it as a big advantage. I think this is also popular in the Golang community, for similar reasons (where ignoring err is all too easy).

1 Like

This seems to be a different warning, unused variable?

that generates error 27:

let f x y = print_string y ;;
5$ ocamlc -w +A foo.ml
File "foo.ml", line 2, characters 6-7:
2 | let f x y = print_string y ;;
Warning 27: unused variable x.

Yes, that’s my understanding, thanks for the example

Yeah, I understand warning 27 is pretty common and useful. My question is about warning 20

Yes, I was replying to @Leonidas, letting them know that their example didn’t generate warning 20, is all. In any case, I have several times written code that triggered this warning, and each time it’s been a bug, waiting to happen.

ETA: triggers this warning in the manner of the example I posted above. That is, a function with multiple args, that can raise an exception after being partially applied.

If you are generating code with Obj.magic, it seems better to add an explicit type constraint
(Obj.magic f: _ -> _ -> _) x y rather than trying to disable the warning.

1 Like

These kind of warning has been very valuable to me when dealing with some state which was modified along the way in a pure calculation. It wasn’t in OCaml but in elm. The concrete piece of code is here: https://slides.com/sebbes/compiler-driven-onboarding/#/75 ; as you can see, it is not obvious at all that I’m doing smthg wrong… This warning enabled me to catch the bug very early in the development process!

(if you want some context, the story starts here: https://slides.com/sebbes/compiler-driven-onboarding/#/57 )

Hi, this is another warning, used variable