From b41e00fd3308b0c28b60bf32fd6c1c59e88cb17f Mon Sep 17 00:00:00 2001 From: Dimi Racordon Date: Mon, 16 Oct 2023 02:30:08 +0200 Subject: [PATCH] Test trait member dispatch --- .../TestCases/TraitMemberDispatch.hylo | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Tests/EndToEndTests/TestCases/TraitMemberDispatch.hylo diff --git a/Tests/EndToEndTests/TestCases/TraitMemberDispatch.hylo b/Tests/EndToEndTests/TestCases/TraitMemberDispatch.hylo new file mode 100644 index 000000000..a55697a19 --- /dev/null +++ b/Tests/EndToEndTests/TestCases/TraitMemberDispatch.hylo @@ -0,0 +1,30 @@ +//- compileAndRun expecting: success + +// This program tests the dispatch behavior of calls to trait members. `P` declares a single +// requirement `f` that is given a default implementation. Two conformances to `P` are defined: +// `Int: P` "overrides" the default implementation of `f` and `Bool: P` uses it. `call_f(on:)` +// accepts a generic parameter bound by `P` and calls `f`. Because it is generic, the compiler +// can't determine how to dispatch `f` before depolymoprhization. + +trait P { + fun f() -> Int +} + +extension P { + public fun f() -> Int { 0 } +} + +conformance Bool: P {} + +conformance Int: P { + public fun f() -> Int { self.copy() } +} + +fun call_f(on x: T) -> Int { + x.f() +} + +public fun main() { + precondition(call_f(on: 1234).f() == 1234) + precondition(call_f(on: true).f() == 0) +}