Skip to content

Change Dim type alias to class AxisExtent - #50

Merged
marcelluethi merged 4 commits into
dimwit-dev:mainfrom
marcelluethi:AxisExtent
Jan 21, 2026
Merged

Change Dim type alias to class AxisExtent#50
marcelluethi merged 4 commits into
dimwit-dev:mainfrom
marcelluethi:AxisExtent

Conversation

@marcelluethi

Copy link
Copy Markdown
Contributor

So far the construction Axis[T]->5 was used to create a tuple (Axis, Int), which was given the type alias Dim.
As this takes a prominent role in the code and is often used to construct new Tensor shapes from old one, keeping Dim as a type alias seems inadequate. This PR therefore proposes to make it a class. As Dim is not a great name, we also propose to call it AxisExtent.

The syntax Axis[T]->5 is nice and should be kept. Therefore an extension method on Axis is introduced to keep it.
As this creates a tension with tuple constructions, that are used in the slice method to specify slicing dimensions and in relabel, these mechanisms are also changed.

For Slicing, we propose to use the syntax ~>, which is another extension method on Axis that creates an AxisRange or AxisIndex. For relabel, an infix method as was added.

The semantics of all the operations remains the same.

Introduce convenience syntax for
-  creating axis extent ->
- creating axis Slices ~>
- renaming as
@marcelluethi
marcelluethi requested a review from benikm91 January 21, 2026 06:55
@benikm91

Copy link
Copy Markdown
Collaborator

Minor adjustments:

  1. First commit: Bugfix in ML example.
  2. Second commit: Remove Axis -> Int support for slice.

I like the distinction between ->, ~> and as as they are different concepts.

I am not sure about the ~> Syntax. And as as an infix operation.

I suggest general non-infix named functions (on Axis)
t.extend(...)
t.as(...)
t.at(...)

t.slice((Axis[C].at(0), Axis[D].at(0)))
t.slice(Axis[B].at(0 until part1.shape(Axis[B])))

and only -> as an infix operation as syntax sugar for common extend() operation

The -> syntax is common/known in Scala due to Tuples creation in PreDef.

@marcelluethi

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I agree with your suggestion and especially like the slicing with at.

@benikm91

Copy link
Copy Markdown
Collaborator

I implemented suggested changes: .extent(...), .at(...), as(...), ->. No longer ~> and _ as _.
Please see commit.

I moved the implementations to trait Axis. If there is a reason to move it to an extension block, (which we export in dimwit), feel free to change.

sealed trait Axis[A]:
  def extent(size: Int): AxisExtent[A] = AxisExtent(this, size)
  def ->(size: Int): AxisExtent[A] = this.extent(size)
  def at(index: Int): AxisAtIndex[A] = AxisAtIndex(this, index)
  def at(range: Range): AxisAtRange[A] = AxisAtRange(this, range)
  def at(indices: Seq[Int]): AxisAtIndices[A] = AxisAtIndices(this, indices)
  def at(index: Tensor0[Int]): AxisAtTensorIndex[A] = AxisAtTensorIndex(this, index)
  def as[U](newAxis: Axis[U]): (Axis[A], Axis[U]) = (this, newAxis)

@marcelluethi
marcelluethi merged commit d9df18f into dimwit-dev:main Jan 21, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants