Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@hearnadam
Copy link
Collaborator

Closes #4315

* non-empty or fails with the error `None` if the list is empty.
*/
final def head[B](implicit ev: A IsSubtypeOfOutput List[B], trace: Trace): ZIO[R, Option[E], B] =
final def head[B](implicit ev: A IsSubtypeOfOutput Seq[B], trace: Trace): ZIO[R, Option[E], B] =
Copy link
Member

Choose a reason for hiding this comment

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

Btw it doesn't impact binary compatibility?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Pretty sure it's okay. We can introduce a private method if you think it's a concern. The bytecode looks like this:

    default public <B> ZIO<R, Option<E>, B> head(IsSubtypeOfOutput<A, Seq<B>> ev, Object trace) {
        return this.foldZIO((Function1<Object, Exit> & Serializable)e -> Exit$.MODULE$.fail(new Some<Object>(e)), (Function1<Object, ZIO> & Serializable)a -> ((IterableOps)ev.apply(a)).headOption().fold((Function0<Exit> & Serializable)() -> Exit$.MODULE$.failNone(), ZIO$.MODULE$.successFn()), CanFail$.MODULE$.canFail(), trace);
    }

The IsSubtypeOf<A, Seq<B>> will be erased, so it shouldn't matter ... which would be problematic if this required changing the body of the function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The evidence is purely to make the compiler happy as well. headOption exists on the Seq interface, which the JVM will traverse available interfaces to find the method to dispatch to anyways.

The evidence provided is non-important (type wise) as it's essentially just identity:

https://github.com/zio/zio/blob/series/2.x/core/shared/src/main/scala/zio/IsSubtype.scala#L11
https://github.com/scala/scala/blob/c6ce350a8e231351256280b0fae9a85bba0fdfbc/src/library/scala/typeConstraints.scala#L152

Copy link
Member

Choose a reason for hiding this comment

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

Nice!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this should be binary compatible as @hearnadam mentioned due to erasure

@hearnadam hearnadam merged commit 28b7aaa into zio:series/2.x Jan 29, 2025
18 checks 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.

Why vector?

3 participants