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 TypedOptions #1417

Merged
merged 5 commits into from
Feb 5, 2024
Merged

Add TypedOptions #1417

merged 5 commits into from
Feb 5, 2024

Conversation

swankjesse
Copy link
Member

No description provided.

*
* Also consider [Options] to select an integer index.
*/
class TypedOptions<T : Any>(
Copy link
Member Author

Choose a reason for hiding this comment

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

Public constructor so we can accept an inline function in TypedOptions.of()


companion object {
@JvmStatic
inline fun <T : Any> of(
Copy link
Member Author

Choose a reason for hiding this comment

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

This API isn’t totally Kotlin-idiomatic, but it’s consistent with Options

Copy link

Choose a reason for hiding this comment

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

We could have it be an invoke operator with a JvmName("of") annotation. I saw that in some other library I think

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. I think keeping it consistent with Options is good.

* limitations under the License.
*/

@file:JvmName("-BufferedSource") // A leading '-' hides this class from Java.
Copy link
Member Author

Choose a reason for hiding this comment

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

This is our first function that uses the same implementation for both Buffer and BufferedSource. It could be an extension function, but I think polymorphic is a little nicer, especially since the other select() is polymorphic

* ```
* TypedOptions<Direction> options = TypedOptions.of(
* Arrays.asList(Direction.values()),
* (direction) -> ByteString.encodeUtf8(direction.name().toLowerCase(Locale.ROOT))
Copy link
Member Author

Choose a reason for hiding this comment

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

Java language example!

Comment on lines 46 to 48
val list = values.toList()
val options = Options.of(*list.map { encode(it) }.toTypedArray<ByteString>())
return TypedOptions(list, options)
Copy link
Member

Choose a reason for hiding this comment

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

The amount of copies that occur in these three lines is staggering. I'm doing my best to suppress the desire to reduce them 🥵

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn’t see anything where reducing allocations was worth the extra code that it’d cost. I think it’s fine for Options to do defensive copies the expensive way. It’d be nice if we had a proper immutable list class instead of our janky unmodifiable list interface.

okio/src/commonMain/kotlin/okio/TypedOptions.kt Outdated Show resolved Hide resolved
Co-authored-by: Jake Wharton <jw@squareup.com>
@swankjesse swankjesse enabled auto-merge (squash) February 5, 2024 21:07
@swankjesse swankjesse merged commit 0d76d41 into master Feb 5, 2024
11 checks passed
@swankjesse swankjesse deleted the jwilson.0204.typed_options branch February 5, 2024 23:07
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.

None yet

4 participants