-
Notifications
You must be signed in to change notification settings - Fork 612
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
[wpiutil] Add Java algebraic data types #7447
Comments
I didn't know Java 21 brought in so many wonderful ADT features. This will be useful almost everywhere in the codebase. For instance the units library. 👍 |
@narmstro2020 I hadn't considered how the units library would be affected. Can you give some examples of how you think it'd be used? |
You could reduce the amount of instanceof's that currently exist in the api. With pattern matching. That's the first thing that came to mind. The entire API is just loaded with types. It really just depends on how well ADTs and pattern matching play well with Java generics or if an alternative can be found. |
Well, measures are technically already product types; they'll just formally become so when we can transition them to records in 2027. I don't see how pattern matching would be useful for them, since you typically use the direct measure types when possible (eg |
Whats preventing records from being used now. I thought they were a Java 14 feature? |
Immutability constraints imposed by the roboRIO. 2027 will have just a single record for measure objects, instead of an interface with an immutable and a mutable implementation. |
The thought was mostly a potential. I was watching a tutorial on ADT's in Java. Remembered your post and had a low level epiphany. It may be for nought. It might be useful it might not. Wanted to share with you either way. |
We currently use Java's
Optional
type to represent optional values, such as alliance station or color. This can still lead to errors in user code, however, since there's no guardrails to prevent users from callingget()
without first checking forisPresent()
. Adding ADTs for optional values would allow us to leverage the type system to put such guardrails in place.This requires Java 21 for its pattern matching support. It would still be possible on Java 17 with its enhanced instanceof and sealed types, but the checks are clunky. Tagging this for 2027 for MRC and Java 21+ support
Types like Rust's
Result
probably shouldn't be added. Java already functionally has that with exceptions, which also force users to either handle the error results or let their program crash; aResult
type would allow users to just ignore errors, which would be a regressionThe text was updated successfully, but these errors were encountered: