I'm new to Nim, so this might be an obtuse question, but how does one create a short-hand alias variable for the purpose of simplifying code?
For instance:
import sdl2
import sdl2.gfx
type
Vector[T] = object
x, y: T
Ball = object
pos: Vector[float]
Game = ref object
renderer: RendererPtr
ball: array[10, Ball]
proc render(game: Game) =
# ...
# Render the balls
for ix in low(game.ball)..high(game.ball):
var ball : ref Ball = game.ball[ix]
game.renderer.filledCircleRGBA(
int16(game.renderer.ball[ix].pos.x),
int16(game.renderer.ball[ix].pos.y),
10, 100, 100, 100, 255)
# ...
Instead of that last part, I'd like to use a shorter alias to access the ball position:
# Update the ball positions
for ix in low(game.ball)..high(game.ball):
??? pos = game.ball[ix].pos
game.renderer.filledCircleRGBA(
int16(pos.x),
int16(pos.y),
10, 100, 100, 100, 255)
However, if I use a var
in place of ???
, then I seem to create a copy in pos
, which then means the original isn't updated. A ref
isn't allowed, and let
won't let me mutate it.
This seems a natural thing to want to do, so I'd be surprised if Nim doesn't let you do it, I just can't see anything in the manuals or tutorials.
[later] Well, apart from "abusing" ptr
to achieve this, but I had thought that use of ptr
is discouraged except for C API interoperability.
What I'm hoping for is something like Lisp/Haskell's let*
construct...