-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81361f5
commit b41e00f
Showing
1 changed file
with
30 additions
and
0 deletions.
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
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) | ||
} |