Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

032-Patterns.cs 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. namespace Burst.Compiler.IL.Tests.Shared
  2. {
  3. internal class Patterns
  4. {
  5. [TestCompiler(2)]
  6. [TestCompiler(1)]
  7. [TestCompiler(0)]
  8. public static int PropertyPattern(int x)
  9. {
  10. var point = new Point { X = x, Y = 5 };
  11. return point switch
  12. {
  13. { X: 2 } => 10,
  14. { X: 1 } => 5,
  15. _ => 0
  16. };
  17. }
  18. private struct Point
  19. {
  20. public int X;
  21. public int Y;
  22. }
  23. [TestCompiler(1, 2)]
  24. [TestCompiler(2, 4)]
  25. [TestCompiler(0, 0)]
  26. public static int TuplePattern(int x, int y)
  27. {
  28. return (x, y) switch
  29. {
  30. (1, 2) => 10,
  31. (2, 4) => 5,
  32. _ => 0
  33. };
  34. }
  35. private struct DeconstructablePoint
  36. {
  37. public int X;
  38. public int Y;
  39. public void Deconstruct(out int x, out int y) => (x, y) = (X, Y);
  40. }
  41. [TestCompiler(1, -1)]
  42. [TestCompiler(-1, 1)]
  43. [TestCompiler(1, 1)]
  44. [TestCompiler(-1, -1)]
  45. public static int PositionalPattern(int pointX, int pointY)
  46. {
  47. var point = new DeconstructablePoint { X = pointX, Y = pointY };
  48. return point switch
  49. {
  50. (0, 0) => 0,
  51. var (x, y) when x > 0 && y > 0 => 1,
  52. var (x, y) when x < 0 && y > 0 => 2,
  53. var (x, y) when x < 0 && y < 0 => 3,
  54. var (x, y) when x > 0 && y < 0 => 4,
  55. var (_, _) => 5
  56. };
  57. }
  58. }
  59. }