-
Notifications
You must be signed in to change notification settings - Fork 94
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
Draft
illusory0x0
wants to merge
2
commits into
moonbitlang:main
Choose a base branch
from
illusory0x0:feat-clone-trait
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
add Clone trait #1438
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
// Copyright 2025 International Digital Economy Academy | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
///| | ||
impl Clone for Unit with mutability() { Immutable } | ||
|
||
///| | ||
impl Clone for Unit with clone(self) { self } | ||
|
||
///| | ||
impl Clone for Bool with mutability() { Immutable } | ||
|
||
///| | ||
impl Clone for Bool with clone(self) { self } | ||
|
||
///| | ||
impl Clone for Int with mutability() { Immutable } | ||
|
||
///| | ||
impl Clone for Int with clone(self) { self } | ||
|
||
///| | ||
impl Clone for Int64 with mutability() { Immutable } | ||
|
||
///| | ||
impl Clone for Int64 with clone(self) { self } | ||
|
||
///| | ||
impl Clone for UInt with mutability() { Immutable } | ||
|
||
///| | ||
impl Clone for UInt with clone(self) { self } | ||
|
||
///| | ||
impl Clone for Byte with mutability() { Immutable } | ||
|
||
///| | ||
impl Clone for Byte with clone(self) { self } | ||
|
||
///| | ||
impl Clone for String with mutability() { Immutable } | ||
|
||
///| | ||
impl Clone for String with clone(self) { self } | ||
|
||
// mutable containers | ||
|
||
///| | ||
fn copy_array[T](self : Array[T]) -> Array[T] { | ||
let len = self.length() | ||
if len == 0 { | ||
[] | ||
} else { | ||
let arr = Array::make(len, self[0]) | ||
Array::unsafe_blit(arr, 0, self, 0, len) | ||
arr | ||
} | ||
} | ||
|
||
///| | ||
impl[T : Clone] Clone for Array[T] with mutability() { Mutable } | ||
|
||
///| | ||
impl[T : Clone] Clone for Array[T] with clone(self) { | ||
match T::mutability() { | ||
Mutable => self.map(T::clone) | ||
Immutable => self.copy_array() | ||
} | ||
} | ||
|
||
test "Array[Int]::clone" { | ||
let xs = [1, 2, 3, 4, 5] | ||
let ys = xs.clone() | ||
xs[0] = 99 | ||
inspect!(xs, content="[99, 2, 3, 4, 5]") | ||
inspect!(ys, content="[1, 2, 3, 4, 5]") | ||
} | ||
|
||
test "Array[Ref[Int]]::clone" { | ||
let xs = [1, 2, 3, 4, 5].map(fn(x) { { val: x } }) | ||
let ys = xs.clone() | ||
xs[0].val = 99 | ||
inspect!(xs, content="[{val: 99}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]") | ||
inspect!(ys, content="[{val: 1}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]") | ||
} | ||
|
||
///| | ||
impl[T : Clone] Clone for FixedArray[T] with mutability() { Mutable } | ||
|
||
///| | ||
fn map_fixedarray[T, U](self : FixedArray[T], f : (T) -> U) -> FixedArray[U] { | ||
if self.length() == 0 { | ||
return [] | ||
} | ||
let res = FixedArray::make(self.length(), f(self[0])) | ||
for i = 1; i < self.length(); i = i + 1 { | ||
res[i] = f(self[i]) | ||
} | ||
res | ||
} | ||
|
||
///| | ||
impl[T : Clone] Clone for FixedArray[T] with clone(self) { | ||
match T::mutability() { | ||
Mutable => self.map_fixedarray(T::clone) | ||
Immutable => self.map_fixedarray(fn(x) { x }) | ||
} | ||
} | ||
|
||
test "FixedArray[Int]::clone" { | ||
let xs : FixedArray[Int] = [1, 2, 3, 4, 5] | ||
let ys = xs.clone() | ||
xs[0] = 99 | ||
inspect!(xs, content="[99, 2, 3, 4, 5]") | ||
inspect!(ys, content="[1, 2, 3, 4, 5]") | ||
} | ||
|
||
test "FixedArray[Ref[Int]]::clone" { | ||
let xs = ([1, 2, 3, 4, 5] : FixedArray[Int]).map_fixedarray(fn(x) { | ||
{ val: x } | ||
}) | ||
let ys = xs.clone() | ||
xs[0].val = 99 | ||
inspect!(xs, content="[{val: 99}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]") | ||
inspect!(ys, content="[{val: 1}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]") | ||
} | ||
|
||
///| | ||
impl[T : Clone] Clone for Ref[T] with mutability() { Mutable } | ||
|
||
///| | ||
impl[T : Clone] Clone for Ref[T] with clone(self) { | ||
match T::mutability() { | ||
Mutable => { val: T::clone(self.val) } | ||
Immutable => { val: self.val } | ||
} | ||
} | ||
|
||
test "Ref[Int]::clone" { | ||
let x = { val: 0 } | ||
let y = x.clone() | ||
x.val = 10 | ||
inspect!(x, content="{val: 10}") | ||
inspect!(y, content="{val: 0}") | ||
} | ||
|
||
///| | ||
impl Clone for Bytes with mutability() { Mutable } | ||
|
||
///| | ||
impl Clone for Bytes with clone(self) { self.copy() } | ||
|
||
test "Bytes::clone" { | ||
let xs = b"\x12\x34" | ||
let ys = xs.clone() | ||
xs[0] = b'\x00' | ||
let xss = | ||
#|b"\x00\x34" | ||
let yss = | ||
#|b"\x12\x34" | ||
inspect!(xs, content=xss) | ||
inspect!(ys, content=yss) | ||
} | ||
|
||
// immutable containers | ||
|
||
///| | ||
impl[T : Clone] Clone for T? with mutability() { T::mutability() } | ||
|
||
///| | ||
impl[T : Clone] Clone for T? with clone(self) { | ||
match T::mutability() { | ||
Mutable => | ||
match self { | ||
Some(x) => Some(T::clone(x)) | ||
None => None | ||
} | ||
Immutable => self | ||
} | ||
} | ||
|
||
test "Ref[Int]?::clone" { | ||
let x = Some({ val: 0 }) | ||
let y = x.clone() | ||
x.unwrap().val = 99 | ||
inspect!(x, content="Some({val: 99})") | ||
inspect!(y, content="Some({val: 0})") | ||
} | ||
|
||
///| | ||
impl[T : Clone, E : Clone] Clone for Result[T, E] with mutability() { | ||
match (T::mutability(), E::mutability()) { | ||
(Immutable, Immutable) => Immutable | ||
_ => Mutable | ||
} | ||
} | ||
|
||
///| | ||
impl[T : Clone, E : Clone] Clone for Result[T, E] with clone(self) { | ||
match T::mutability() { | ||
Mutable => | ||
match self { | ||
Ok(x) => Ok(T::clone(x)) | ||
Err(x) => Err(E::clone(x)) | ||
} | ||
Immutable => self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also need to check E::mutability() here. I'd suggest
|
||
} | ||
} | ||
|
||
test "Result[Ref[Int],Ref[String]]::clone" { | ||
let x : Result[Ref[Int], Ref[String]] = Ok({ val: 1 }) | ||
let y = x.clone() | ||
match x { | ||
Ok(x) => x.val = 99 | ||
Err(x) => x.val = "OvO" | ||
} | ||
inspect!(x, content="Ok({val: 99})") | ||
inspect!(y, content="Ok({val: 1})") | ||
let x : Result[Ref[Int], Ref[String]] = Err({ val: "mooncake" }) | ||
let y = x.clone() | ||
match x { | ||
Ok(x) => x.val = 99 | ||
Err(x) => x.val = "OvO" | ||
} | ||
inspect!(x, content="Err({val: \"OvO\"})") | ||
inspect!(y, content="Err({val: \"mooncake\"})") | ||
} | ||
|
||
///| | ||
fn get_mutability[T : Clone](_ : T) -> Mutability { | ||
T::mutability() | ||
} | ||
|
||
test "mutability" { | ||
|
||
// immutable | ||
let oi = Some(0) | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have a function |
||
|
||
// mutable | ||
let ri : Ref[Int] = { val: 0 } | ||
let ari = [ri] | ||
let aoi = [oi] | ||
inspect!(get_mutability(ri), content="Mutable") | ||
inspect!(get_mutability(ari), content="Mutable") | ||
inspect!(get_mutability(aoi), content="Mutable") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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