Skip to content

Commit

Permalink
Test trait member dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
kyouko-taiga committed Oct 16, 2023
1 parent 81361f5 commit b41e00f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Tests/EndToEndTests/TestCases/TraitMemberDispatch.hylo
Original file line number Diff line number Diff line change
@@ -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<T: P>(on x: T) -> Int {
x.f()
}

public fun main() {
precondition(call_f(on: 1234).f() == 1234)
precondition(call_f(on: true).f() == 0)
}

0 comments on commit b41e00f

Please sign in to comment.