From f9fcaebccdc0f9dbe46a90307ddbb70315e5af12 Mon Sep 17 00:00:00 2001 From: Brian Grenier Date: Mon, 1 Jul 2024 12:45:46 -0600 Subject: [PATCH] Make slice step optional Signed-off-by: Brian Grenier --- stdlib/src/builtin/builtin_slice.mojo | 10 +++++----- stdlib/test/builtin/test_slice.mojo | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/stdlib/src/builtin/builtin_slice.mojo b/stdlib/src/builtin/builtin_slice.mojo index 48958291a1b..c258f55a21e 100644 --- a/stdlib/src/builtin/builtin_slice.mojo +++ b/stdlib/src/builtin/builtin_slice.mojo @@ -44,7 +44,7 @@ struct Slice( """The starting index of the slice.""" var end: Optional[Int] """The end index of the slice.""" - var step: Int + var step: Optional[Int] """The step increment value of the slice.""" @always_inline("nodebug") @@ -57,7 +57,7 @@ struct Slice( """ self.start = start self.end = end - self.step = 1 + self.step = None @always_inline("nodebug") fn __init__( @@ -75,7 +75,7 @@ struct Slice( """ self.start = start self.end = end - self.step = step.or_else(1) + self.step = step fn __init__(inout self, *, other: Self): """Creates a deep copy of the Slice. @@ -126,7 +126,7 @@ struct Slice( writer.write(", ") write_optional(self.end) writer.write(", ") - writer.write(repr(self.step)) + write_optional(self.step) writer.write(")") @always_inline("nodebug") @@ -191,7 +191,7 @@ struct Slice( Returns: A tuple containing three integers for start, end, and step. """ - var step = self.step + var step = self.step.or_else(1) var start = self.start var end = self.end diff --git a/stdlib/test/builtin/test_slice.mojo b/stdlib/test/builtin/test_slice.mojo index 7736dae2363..b552d13f0b8 100644 --- a/stdlib/test/builtin/test_slice.mojo +++ b/stdlib/test/builtin/test_slice.mojo @@ -19,7 +19,7 @@ def test_none_end_folds(): var all_def_slice = slice(0, None, 1) assert_equal(all_def_slice.start.value(), 0) assert_true(all_def_slice.end is None) - assert_equal(all_def_slice.step, 1) + assert_equal(all_def_slice.step.value(), 1) # This requires parameter inference of StartT. @@ -74,11 +74,11 @@ def test_slice_stringable(): var s = SliceStringable() assert_equal(s[2::-1], "slice(2, None, -1)") assert_equal(s[1:-1:2], "slice(1, -1, 2)") - assert_equal(s[:-1], "slice(None, -1, 1)") - assert_equal(s[::], "slice(None, None, 1)") + assert_equal(s[:-1], "slice(None, -1, None)") + assert_equal(s[::], "slice(None, None, None)") assert_equal(s[::4], "slice(None, None, 4)") assert_equal(repr(slice(None, 2, 3)), "slice(None, 2, 3)") - assert_equal(repr(slice(10)), "slice(None, 10, 1)") + assert_equal(repr(slice(10)), "slice(None, 10, None)") def test_slice_eq():