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

Cookbook: Debug Print a Value with ppx_deriving #2917

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mostafatouny
Copy link

@mostafatouny mostafatouny commented Jan 9, 2025

Hello,

I am contributing to Debug Print a Value.

Moreover,

  • I hope someone shows how to use ppx_deriving with utop -ppx.
  • I nominate @yawaramin to add his post, as he commented here.
  • I hope someone add printing recipes for maps (association table), Sets, Hash-tables, and Sequences data structures.

Copy link
Collaborator

@cuihtlauac cuihtlauac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mostafatouny

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mostafatouny. I just wanted to inform you that your comments don't provide helpful information. This needs to be fixed.
image

The discussion block does not need project creation instruction. This is covered elsewhere.

@sabine sabine changed the title debug print a value with ppx Cookbook: Debug Print a Value with ppx_deriving Jan 17, 2025
removed project initialization. added more info to comments.
@mostafatouny
Copy link
Author

Thank you @cuihtlauac and @sabine for your time. I removed the project initialization and added more info to the comments.

@cuihtlauac
Copy link
Collaborator

Thanks for the quick update, @mostafatouny. That's nice. I'm under the impression this recipe has more to do with using ppx_deriving.show than debugging. The former is useful in the latter context, but that's the only relationship I see. I propose we remove all references to debugging and change the title to produce value literals using preprocessing (or something akin).

@yawaramin
Copy link
Contributor

yawaramin commented Jan 17, 2025

@cuihtlauac there is a connection to debugging. People often want to debug by just printing the values. Then they have to figure out how to print the values. And they see that they need to derive printers for custom types. So we should definitely mention this workflow.

If we don't mention a motivating use case like debugging, it might not be clear to people why they would want to use this recipe. The recipe should justify itself by providing a use case. It's not enough to just show how to do it.

@cuihtlauac
Copy link
Collaborator

Thanks, @yawaramin. You have a valid point. The key is discovering that you need ppx show when debugging. However, show is also very useful in multi-staged compilation. Maybe this could be mentioned in the discussion.


(* string *)
let () = print_endline "hello world"
let () = print_endline ([%show: string] "hello world")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems redundant. The string literal is already a string.

let () = print_endline ([%show: string] "hello world")

(* integer *)
let show_int = [%show: int]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slightly more useful but no clear advantage over string_of_int imho.

type tree =
| Leaf of float
| Node of float * tree * tree
[@@deriving show]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to explain why we are suddenly switching from [%show: ...] to [@@deriving show].

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It helps to take inspiration from the JSON parsing recipes, to give a better explanation of what @@deriving does.

And indeed, makes sense to add a single sentence that explains that [%show: t] generates the code to print values of type t

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sabine would you recommend me background resources to check? maybe I can write a short explanation and enhance the tutorial's accessibility.

@mostafatouny
Copy link
Author

mostafatouny commented Jan 18, 2025

The key is discovering that you need ppx show when debugging

I agree. If something is well-documented but end users cannot figure it is the solution to their problem, then it is useless.

However, show is also very useful in multi-staged compilation

We can create
1 - a cookbook guide about ppx_deriving.
2 - small example on Debug Print, citing guide (1).
3 - small example on multi-staged compilation, citing guide (1).


Thanks @yawaramin for the code feedback. I removed redundant string and int. As a beginner myself, I cannot explain why we need @@deriving.

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.

4 participants