match_first.carbon 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // CHECK:STDOUT: XYZ: 1
  7. // CHECK:STDOUT: XY: 1
  8. // CHECK:STDOUT: XZ: 1
  9. // CHECK:STDOUT: YZ: 2
  10. // CHECK:STDOUT: JustX: 1
  11. // CHECK:STDOUT: JustY: 2
  12. // CHECK:STDOUT: JustZ: 3
  13. // CHECK:STDOUT: None: 4
  14. // CHECK:STDOUT: result: 0
  15. package ExplorerTest api;
  16. interface A {
  17. fn Which() -> i32;
  18. }
  19. interface X {}
  20. interface Y {}
  21. interface Z {}
  22. __match_first {
  23. external impl forall [T:! X] T as A {
  24. fn Which() -> i32 { return 1; }
  25. }
  26. external impl forall [T:! Y] T as A {
  27. fn Which() -> i32 { return 2; }
  28. }
  29. external impl forall [T:! Z] T as A {
  30. fn Which() -> i32 { return 3; }
  31. }
  32. external impl forall [T:! type] T as A {
  33. fn Which() -> i32 { return 4; }
  34. }
  35. }
  36. class XYZ {}
  37. class XY {}
  38. class XZ {}
  39. class YZ {}
  40. class JustX {}
  41. class JustY {}
  42. class JustZ {}
  43. class None {}
  44. impl XYZ as X & Y & Z {}
  45. impl XY as X & Y {}
  46. impl XZ as X & Z {}
  47. impl YZ as Y & Z {}
  48. impl JustX as X {}
  49. impl JustY as Y {}
  50. impl JustZ as Z {}
  51. fn Main() -> i32 {
  52. Print("XYZ: {0}", XYZ.(A.Which)());
  53. Print("XY: {0}", XY.(A.Which)());
  54. Print("XZ: {0}", XZ.(A.Which)());
  55. Print("YZ: {0}", YZ.(A.Which)());
  56. Print("JustX: {0}", JustX.(A.Which)());
  57. Print("JustY: {0}", JustY.(A.Which)());
  58. Print("JustZ: {0}", JustZ.(A.Which)());
  59. Print("None: {0}", None.(A.Which)());
  60. return 0;
  61. }