Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Future.discard extension method #60

Merged
merged 1 commit into from
Sep 24, 2024

Conversation

isaacl
Copy link
Member

@isaacl isaacl commented Sep 21, 2024

This method can be used to mark Future values that we're explicitly ignoring, to enable better unused value detection.

@isaacl
Copy link
Member Author

isaacl commented Sep 21, 2024

@lenguyenthanh ^ for adding unused checks to lila

@ornicar
Copy link
Collaborator

ornicar commented Sep 22, 2024

scalalib already provides future.void and its type is Future[Unit] https://github.com/lichess-org/scalalib/blob/master/lila/src/main/scala/future.scala#L33

@isaacl
Copy link
Member Author

isaacl commented Sep 22, 2024

Ah, I didn't notice this. But I'm not sure this void impl helps with unused object false positives, because the type is still non-unit and there's places in lichess where we 'fire and forget' Futures and scala thinks we're discarding data accidentally.

@lenguyenthanh would be able to comment more. I saw it mentioned in zulip but I can't find the thread anymore.

@ornicar
Copy link
Collaborator

ornicar commented Sep 22, 2024

extension [A](a: A) def discard: Unit = ()

would allow discarding any value and returning unit by adding .discard. As an alternative to ; ().

@lenguyenthanh
Copy link
Member

lenguyenthanh commented Sep 23, 2024

this is the thread that @isaacl mentioned: https://hq.lichess.ovh/#narrow/stream/8-dev/topic/scala.20detection.20of.20dangling.20code

I think discard (or maybe ignored, effectful) is a better name for this function if We want to go this way. Which I believe is a way to avoid the problem instead of solving problem of dangling code. But I'm biased about purity.

@isaacl
Copy link
Member Author

isaacl commented Sep 23, 2024

@lenguyenthanh Would discard or similar still be useful as an intermediate step to allow us to quickly enable unused detection? I'm not familiar enough with all the places we're ignore futures to tell how easy it would be to fix these. What do you think?

@lenguyenthanh
Copy link
Member

I'm not familiar enough with all the places we're ignore futures to tell how easy it would be to fix these. What do you think?

It's quite ubiquitous in our code base, my gut feelings is about few hundreds. tbh, I'm not sure 🤷 . Maybe it's worth trying.

If We try, we can just add this extension function to lila and apply zero waste plugin and see how it go.

Example in lila-search: https://github.com/ghik/zerowaste (which is 100% pure I don't have to do any massaging)

@isaacl
Copy link
Member Author

isaacl commented Sep 24, 2024

OK, I'll adjust this PR to add .discard. Although the paradigm of <expr producing Future>; () might also trick zerowaste, it wouldn't be as findable for later removal as explicitly calling a .discard method.

This method can be used to mark Future values that
we're explicitly ignoring, to enable better unused
value detection.
@isaacl isaacl changed the title Add Future.void extension method Add Future.discard extension method Sep 24, 2024
@ornicar ornicar merged commit 0c4183f into lichess-org:master Sep 24, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants