match_first.carbon 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. package ExplorerTest api;
  7. interface A {
  8. fn Which() -> i32;
  9. }
  10. interface X {}
  11. interface Y {}
  12. interface Z {}
  13. __match_first {
  14. impl forall [T:! X] T as A {
  15. fn Which() -> i32 { return 1; }
  16. }
  17. impl forall [T:! Y] T as A {
  18. fn Which() -> i32 { return 2; }
  19. }
  20. impl forall [T:! Z] T as A {
  21. fn Which() -> i32 { return 3; }
  22. }
  23. impl forall [T:! type] T as A {
  24. fn Which() -> i32 { return 4; }
  25. }
  26. }
  27. class XYZ {}
  28. class XY {}
  29. class XZ {}
  30. class YZ {}
  31. class JustX {}
  32. class JustY {}
  33. class JustZ {}
  34. class None {}
  35. impl XYZ as X & Y & Z {}
  36. impl XY as X & Y {}
  37. impl XZ as X & Z {}
  38. impl YZ as Y & Z {}
  39. impl JustX as X {}
  40. impl JustY as Y {}
  41. impl JustZ as Z {}
  42. fn Main() -> i32 {
  43. Print("XYZ: {0}", XYZ.(A.Which)());
  44. Print("XY: {0}", XY.(A.Which)());
  45. Print("XZ: {0}", XZ.(A.Which)());
  46. Print("YZ: {0}", YZ.(A.Which)());
  47. Print("JustX: {0}", JustX.(A.Which)());
  48. Print("JustY: {0}", JustY.(A.Which)());
  49. Print("JustZ: {0}", JustZ.(A.Which)());
  50. Print("None: {0}", None.(A.Which)());
  51. return 0;
  52. }
  53. // CHECK:STDOUT: XYZ: 1
  54. // CHECK:STDOUT: XY: 1
  55. // CHECK:STDOUT: XZ: 1
  56. // CHECK:STDOUT: YZ: 2
  57. // CHECK:STDOUT: JustX: 1
  58. // CHECK:STDOUT: JustY: 2
  59. // CHECK:STDOUT: JustZ: 3
  60. // CHECK:STDOUT: None: 4
  61. // CHECK:STDOUT: result: 0