Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat!: Ord と重複する compare 関数を消す
Int.compare, Int64.compare, Duration.compare, Instant.compare を消す.
これらの型は Ord を満たすので,メソッド呼び出し n.compare(m) が常に
E0335 になっていた(proposals/remove-compare.md, #468).min, max と
TestClock は Ord.compare を呼ぶ.

methods__method_call_errors のスナップショットも撮り直す(CI の失敗).
  • Loading branch information
Allianaab2m committed Oct 28, 2026
commit e58ccf67184bf97a1400efb713c027488ed62064
6 changes: 3 additions & 3 deletions crates/emela-codegen-c/tests/programs/core_numeric.emel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn main() {
assert Int.compare((-2147483647 - 1), 2147483647) == Less
assert Int.compare(2, 1) == Greater
assert Int.compare(1, 1) == Equal
assert Ord.compare((-2147483647 - 1), 2147483647) == Less
assert Ord.compare(2, 1) == Greater
assert Ord.compare(1, 1) == Equal
assert Int.min(-2, 3) == -2
assert Int.max(-2, 3) == 3
assert Int.abs(-3) == 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Std.Set.{Set}

type Key(value: Int)
impl Key: Eq { fn eq(self, other) { self.value % 10 == other.value % 10 } }
impl Key: Ord { fn compare(self, other) { Int.compare(self.value % 10, other.value % 10) } }
impl Key: Ord { fn compare(self, other) { Ord.compare(self.value % 10, other.value % 10) } }

pub fn collections(n: Int) -> Int {
old = Map.from_list([(3, n + 3), (1, n + 1), (2, n + 2)])
Expand Down
6 changes: 3 additions & 3 deletions crates/emela-codegen-emb/tests/release_0_24/helpers.emel
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn list_helpers(n: Int) -> Int {
(indexes, items) = List.unzip(enumerated)
assert(indexes == [0, 1, 2])
assert(items == List.take(values, 3))
sorted = List.sort_by([(1, n), (0, n + 1), (1, n + 2)], fn((a, _), (b, _)) { Int.compare(a, b) })
sorted = List.sort_by([(1, n), (0, n + 1), (1, n + 2)], fn((a, _), (b, _)) { Ord.compare(a, b) })
assert(sorted == [(0, n + 1), (1, n), (1, n + 2)])
assert(List.sort([3, 1, 2, 1]) == [1, 1, 2, 3])
List.fold(items, from: 0, f: fn(acc, value) { acc + value })
Expand All @@ -44,13 +44,13 @@ pub fn ascii(text: String) -> String {
}

pub fn numeric(n: Int) -> Int {
assert(Int.compare(n, n) == Equal)
assert(Ord.compare(n, n) == Equal)
assert(Int.min(n, n) == n)
assert(Int.max(n, n) == n)
Int.abs(n)
}
pub fn numeric64(n: Int64) -> Int64 {
assert(Int64.compare(n, n) == Equal)
assert(Ord.compare(n, n) == Equal)
Int64.abs(Int64.min(n, Int64.max(n, n)))
}
pub fn numeric_float(x: Float) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/emela-codegen-emb/tests/release_0_24/traits.emel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Std.Cause.{Cause}

type Key(value: Int)
impl Key: Eq { fn eq(self, other) { self.value % 10 == other.value % 10 } }
impl Key: Ord { fn compare(self, other) { Int.compare(self.value % 10, other.value % 10) } }
impl Key: Ord { fn compare(self, other) { Ord.compare(self.value % 10, other.value % 10) } }
impl Key: Show { fn show(self) { "K#{self.value}" } }

type Envelope(key: Key, text: String) derive Eq, Ord, Show
Expand Down
15 changes: 2 additions & 13 deletions crates/emela-core/lib/core/int.emel
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,9 @@ pub fn shift_right(n: Int, by: Int) -> Int
@intrinsic
pub fn shift_right_unsigned(n: Int, by: Int) -> Int

## Returns Less, Equal or Greater without subtracting (which could overflow).
pub fn compare(a: Int, b: Int) -> Ordering {
if a < b {
Less
} else if a > b {
Greater
} else {
Equal
}
}

## Returns the larger value, or `a` when equal.
pub fn max(a: Int, b: Int) -> Int {
compare(a, b) match {
Ord.compare(a, b) match {
Less -> b
Greater -> a
Equal -> a
Expand All @@ -74,7 +63,7 @@ pub fn max(a: Int, b: Int) -> Int {

## Returns the smaller value, or `a` when equal.
pub fn min(a: Int, b: Int) -> Int {
compare(a, b) match {
Ord.compare(a, b) match {
Less -> a
Greater -> b
Equal -> a
Expand Down
15 changes: 2 additions & 13 deletions crates/emela-core/lib/core/int64.emel
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,17 @@ pub fn shift_right(n: Int64, by: Int) -> Int64
@intrinsic
pub fn shift_right_unsigned(n: Int64, by: Int) -> Int64

## Less, Equal or Greater as `a` is less than, equal to or greater than `b`.
pub fn compare(a: Int64, b: Int64) -> Ordering {
if a < b {
Less
} else if a > b {
Greater
} else {
Equal
}
}

## The first argument if they are equal.
pub fn min(a: Int64, b: Int64) -> Int64 {
compare(a, b) match {
Ord.compare(a, b) match {
Greater -> b
_ -> a
}
}

## The first argument if they are equal.
pub fn max(a: Int64, b: Int64) -> Int64 {
compare(a, b) match {
Ord.compare(a, b) match {
Less -> b
_ -> a
}
Expand Down
2 changes: 1 addition & 1 deletion crates/emela-core/lib/std/clock.emel
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub handler TestClock: Clock {
at = Instant.add(st.get(), Duration.milliseconds(Int64.from_int(if ms < 0 { 0 } else { ms })))
when_stalled(Instant.to_unix_milliseconds(at))
st.update(fn(t) {
Instant.compare(t, at) match {
Ord.compare(t, at) match {
Less -> at
_ -> t
}
Expand Down
10 changes: 0 additions & 10 deletions crates/emela-core/lib/std/duration.emel
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ pub fn subtract(d: Duration, other: Duration) -> Duration {
Duration(milliseconds: d.milliseconds - other.milliseconds)
}

pub fn compare(d: Duration, other: Duration) -> Ordering {
if d.milliseconds < other.milliseconds {
Less
} else if d.milliseconds > other.milliseconds {
Greater
} else {
Equal
}
}

## Milliseconds with an `ms` suffix (`1500ms`).
pub fn to_string(d: Duration) -> String {
"#{Int64.to_string(d.milliseconds)}ms"
Expand Down
11 changes: 0 additions & 11 deletions crates/emela-core/lib/std/instant.emel
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ pub fn since(t: Instant, earlier: Instant) -> Duration {
Duration.milliseconds(t.milliseconds - earlier.milliseconds)
}

## Earlier instants are smaller.
pub fn compare(t: Instant, other: Instant) -> Ordering {
if t.milliseconds < other.milliseconds {
Less
} else if t.milliseconds > other.milliseconds {
Greater
} else {
Equal
}
}

## UTC in ISO 8601 (`2026-10-12T03:04:05.678Z`). Same format as JS `Date.prototype.toISOString`:
## years outside 0 to 9999 are written with a sign and six digits (`+010000`, `-000001`).
pub fn to_string(t: Instant) -> String {
Expand Down
6 changes: 3 additions & 3 deletions crates/emela-driver/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ fn lists_strings_and_int64_run_through_core() {
dbg(String.to_ascii_uppercase("Hello, äb 東 @[`{"))
big = Int64.from_int(2147483647) * Int64.from_int(4)
one = Int64.from_int(1)
dbg((Int64.compare(big, one), Int64.min(big, one), Int64.max(big, one), Int64.abs(-big)))
dbg((big.compare(one), Int64.min(big, one), Int64.max(big, one), Int64.abs(-big)))
}
"##,
);
Expand Down Expand Up @@ -1148,14 +1148,14 @@ fn main() {
c.print_line(Duration.to_string(d))
c.print_line(Int64.to_string(Duration.to_seconds(Duration.milliseconds(-1500))))
c.print_line(Duration.to_string(Duration.subtract(Duration.hours(1), Duration.minutes(61))))
c.print_line(show(Duration.compare(Duration.seconds(1), Duration.milliseconds(1000))))
c.print_line(show(Duration.seconds(1).compare(Duration.milliseconds(1000))))
c.print_line(if Duration.seconds(1) == Duration.milliseconds(1000) { "eq" } else { "ne" })
start = Instant.from_unix_milliseconds(1760238245678)
later = start |> Instant.add(Duration.hours(25))
c.print_line(Instant.to_string(start))
c.print_line(Instant.to_string(later))
c.print_line(Duration.to_string(later |> Instant.since(start)))
c.print_line(show(Instant.compare(start, later)))
c.print_line(show(start.compare(later)))
c.print_line(Int64.to_string(Instant.to_unix_milliseconds(Instant.subtract(start, Duration.milliseconds(678)))))
c.print_line(Instant.to_string(Instant.from_unix_milliseconds(0)))
c.print_line(Instant.to_string(Instant.from_unix_milliseconds(-1)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/emela-types/tests/methods.rs
assertion_line: 149
expression: "render_bodies(r#\"\ntype User(name: String)\n\nfn rename(u: User, name: String) -> User {\n User(name:, ..u)\n}\n\nfn too_few(u: User) {\n u.rename()\n}\n\nfn not_called(u: User) {\n u.rename\n}\n\nfn misspelled(u: User) {\n u.renmae(\"a\")\n}\n\nfn tuple(p: (Int, Int)) {\n p.first()\n}\n\nfn unknown() {\n fn(x) { x.rename(\"a\") }\n}\n\"#)"
---
rename: fn(User, String) -> User
Expand All @@ -17,6 +18,6 @@ unknown: ∀ A. fn() -> fn(A) -> {unknown}
x: ?1
`u.rename()` WrongArgCount { callee: Some("rename"), expected: 1, found: 0 }
`rename` MethodNotCalled { ty: Named(Idx::<TyConData>(3), []), name: "rename", receiver: Some("u") }
`renmae` NoFieldOrFunction { ty: Named(Idx::<TyConData>(3), []), name: "renmae" }
`renmae` no field or function `renmae` for type `User`
`first` type `(Int, Int)` has no fields (`first`)
`rename` FieldOnUnknownType { field: "rename", call: true }
Loading