Skip to content

[docs] Add a blog post about type classes #4913

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

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

Conversation

jackkoenig
Copy link
Contributor

@jackkoenig jackkoenig commented May 2, 2025

Contributor Checklist

Type of Improvement

  • Documentation or website-related

Desired Merge Strategy

  • Squash

Release Notes

Reviewer Checklist (only modified by reviewer)

  • Did you add the appropriate labels? (Select the most appropriate one based on the "Type of Improvement")
  • Did you mark the proper milestone (Bug fix: 3.6.x, 5.x, or 6.x depending on impact, API modification or big change: 7.0)?
  • Did you review?
  • Did you check whether all relevant Contributor checkboxes have been checked?
  • Did you do one of the following when ready to merge:
    • Squash: You/ the contributor Enable auto-merge (squash) and clean up the commit message.
    • Merge: Ensure that contributor has cleaned up their commit history, then merge with Create a merge commit.

@jackkoenig jackkoenig added the Documentation Only changing documentation label May 2, 2025
@jackkoenig jackkoenig requested review from seldridge and mwachs5 May 2, 2025 23:54
```scala
val clients1 = IO(Vec(4, Flipped(new ArbiterClient)))
val arbiter1 = Module(
new GenericPriorityArbiter(4, new ArbiterClient)(_.request, (c, g) => c.grant := g)
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if it might be helpful to show a version of this with the arguments explicitly called out by name. Generally when showing big code blocks it can be helpful to try to draw people's attention to the most relevant bits. Then we could say something like "we can make this shorter / more scala-y by writing it like this :... but it's still pretty clunky"

Effectively, we have taken the two arguments to the arbiter and turned them into methods on the type class.
This looks similar to the proposed object-oriented version of `Arbitrable` above, but note how it is parameterized by the type of the client and accepts the client as an argument.

We can then provide instances of this type class for specific types. For example, for `ArbiterClient` and `Decoupled`:
Copy link
Contributor

@mwachs5 mwachs5 May 4, 2025

Choose a reason for hiding this comment

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

Suggested change
We can then provide instances of this type class for specific types. For example, for `ArbiterClient` and `Decoupled`:
We can then define subclasses (or implementations?) of this type class for specific types. For example, for `ArbiterClient` and `Decoupled`:

Why do we call this an "instance". This is just a class definition. one would have to instantiate it to make it an instance. I think later we will make default implicit instances, but we haven't done that yet, right?

At least we aren't repeating logic anymore, instead we get to just refer to the type class instance.

However, we can do even better.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sort of wondering why we even need to mention "type class" at all, up until this point. Like you could describe the pattern first, then say "by the way, this pattern of making a trait that is parameterized by type, and all its methods are parameterized by type, we call 'type class'"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I think that's a good suggestion.

```

This is equivalent to the previous definition, but is more concise.
Note that unlike the version with the implicit argument, this one does not bind a variable name for the implicit argument.
Copy link
Contributor

Choose a reason for hiding this comment

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

is this really relevant for the blog post? is this something people are going to need to do?

implicit def decoupledArbitrable[T <: Data]: Arbitrable[DecoupledIO[T]] = ...
}
```

Copy link
Contributor

Choose a reason for hiding this comment

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

Or if you are defining a new type you want to make arbitrable, you could include it in the package object for that type

Isn't this the more common case? Can we show it?

@mwachs5
Copy link
Contributor

mwachs5 commented May 4, 2025

not sure what the milestone should be, when did we introduce the blog to the website

@jackkoenig jackkoenig force-pushed the jackkoenig/arbiter-typeclass branch from 40d7508 to 29ad289 Compare May 16, 2025 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Only changing documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants