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 Clone trait #1438

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

Conversation

illusory0x0
Copy link
Contributor

I apologize for turning off the previous pull request by mistake.

Copy link

peter-jerry-ye-code-review bot commented Jan 8, 2025

‼️ This code review is generated by a bot. Please verify the content before trusting it.

Here are three observations and potential issues from the provided git diff output:

  1. Inconsistent Copyright Year:

    • In builtin/traits.mbt, the copyright year was updated from 2024 to 2025. However, the copyright year in the newly created file builtin/clone.mbt is already set to 2025. This inconsistency might not cause functional issues, but it could lead to confusion or legal discrepancies. It would be better to ensure all files have the same copyright year.
  2. Redundant Enum Definition:

    • The Mutability enum is defined twice: once in builtin/builtin.mbt and again in builtin/traits.mbt. This redundancy could lead to maintenance issues or conflicts if the definitions diverge in the future. It would be better to define the Mutability enum in a single location and import it where needed.
  3. Potential Issue with clone Implementation for Array[T]:

    • In builtin/clone.mbt, the clone implementation for Array[T] uses self.copy_array() when T::mutability() is Immutable. However, the copy_array function is defined to create a new array and copy elements, which might not be necessary for immutable types. This could lead to unnecessary performance overhead. It might be more efficient to directly return self for immutable types, as they do not need to be copied.

These issues should be reviewed and addressed to improve code quality and maintainability.

@coveralls
Copy link
Collaborator

coveralls commented Jan 8, 2025

Pull Request Test Coverage Report for Build 4692

Details

  • 29 of 50 (58.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.1%) to 82.844%

Changes Missing Coverage Covered Lines Changed/Added Lines %
builtin/clone.mbt 29 50 58.0%
Totals Coverage Status
Change from base Build 4690: -0.1%
Covered Lines: 4795
Relevant Lines: 5788

💛 - Coveralls

@illusory0x0 illusory0x0 force-pushed the feat-clone-trait branch 12 times, most recently from 2be50ba to 89c90c3 Compare January 8, 2025 12:53
}

///|
impl Clone for Bytes with mutability() { Mutable }
Copy link
Collaborator

Choose a reason for hiding this comment

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

We will switch to immutable @Yu-zh

Ok(x) => Ok(T::clone(x))
Err(x) => Err(E::clone(x))
}
Immutable => self
Copy link
Collaborator

Choose a reason for hiding this comment

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

You also need to check E::mutability() here. I'd suggest

match self {
  Ok(x) => if T is mutable ...
  Err(x) => if E is mutable ...
}

inspect!(get_mutability(oi), content="Immutable")
let ris : Result[Int, String] = Ok(32)
inspect!(get_mutability(ris), content="Immutable")
// TODO, we need to test reference equal
Copy link
Collaborator

Choose a reason for hiding this comment

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

we have a function physical_equal but be aware that its behavior is backend-dependent.

@bobzhang
Copy link
Contributor

bobzhang commented Jan 9, 2025

what's the motivation for this API? it looks to me we are adding lots of API for a niche use case?

@illusory0x0
Copy link
Contributor Author

what's the motivation for this API? it looks to me we are adding lots of API for a niche use case?

@FlammeShadow
Copy link
Contributor

FlammeShadow commented Jan 9, 2025

It seems the type Mutability is only used by mutability(). Why not just use is_mutable() -> Bool ?

@illusory0x0
Copy link
Contributor Author

illusory0x0 commented Jan 9, 2025

stm32 use RESET SET, ENABLE DISABLE
enum, rather than only true and false.

add new enum for type safe, and more meaningful

It seems the type Mutability is only used by mutability(). Why not just use is_mutable(Self) -> Bool ?

... this might be api preference

this is just compiletime constant, not depend instance, below is enough
is_mutable() -> Bool

@FlammeShadow
Copy link
Contributor

this is just compiletime constant, not depend instance, below is enough
is_mutable() -> Bool

My fault. It was my mistake.

@illusory0x0
Copy link
Contributor Author

cc @Guest0x0

@Lampese
Copy link
Collaborator

Lampese commented Jan 10, 2025

what's the motivation for this API? it looks to me we are adding lots of API for a niche use case?

cc @bobzhang @illusory0x0

Is this referring to discussing the necessity of the mutable API rather than the necessity of the Clone trait?

I think the original intention of distinguishing between immutable and mutable in the design was because the Clone behavior of immutable is unreasonable, but there may be some problems with the API design. Users are already explicitly using this API when using Clone, which proves that the cost of deep replication of immutable data structures is visible and affordable for users (we do have such a need, if there is a lot of mutable data stored in an immutable data structure) Sacrificing convenience to distinguish the need for variability may not be ideal.

@illusory0x0
Copy link
Contributor Author

Is this referring to discussing the necessity of the mutable API rather than the necessity of the Clone trait?

cc @Lampese

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.

7 participants