我是 Nim 的新手,所以这可能是一个迟钝的问题,但是如何创建一个简写别名变量以简化代码?
例如:
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)
# ...
代替最后一部分,我想使用较短的别名来访问球的位置:
# 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)
但是,如果我使用var
代替???
,那么我似乎在pos
中创建了一个副本,这意味着原始副本不会更新。ref
是不允许的,let
也不会让我变异它。
这似乎是一件很自然的事情,所以如果 Nim 不让你这样做,我会感到惊讶,我只是在手册或教程中看不到任何东西。
[后来] 好吧,除了"滥用"ptr
来实现这一点之外,但我曾认为除了 C API 互操作性之外,不鼓励使用ptr
。
我希望的是类似于Lisp/Haskell的let*
构造......
另一种解决方案,也许更像 Nim,是使用模板。Nim 中的模板只是 AST 级别的简单替换。因此,如果您创建几个这样的模板:
template posx(index: untyped): untyped = game.ball[index].pos.x.int16
template posy(index: untyped): untyped = game.ball[index].pos.y.int16
现在,您可以将代码替换为:
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(
posx(ix),
posy(ix),
10, 100, 100, 100, 255)
这将在编译时转换为原始代码,并且不会产生任何开销。它还将保持与原始代码相同的类型安全性。
当然,如果您发现自己经常这样做,则可以创建一个模板来创建模板:
template alias(newName: untyped, call: untyped) =
template newName(): untyped = call
然后可以在代码中像这样使用它:
proc render(game: Game) =
# Render the balls
for ix in low(game.ball)..high(game.ball):
var ball : ref Ball = game.ball[ix]
alias(posx, game.ball[ballIndex].pos.x.int16)
alias(posy, game.ball[ballIndex].pos.y.int16)
game.renderer.filledCircleRGBA(
posx(ix),
posy(ix),
10, 100, 100, 100, 255)
如您所见,该解决方案只有在多次使用时才真正有用。另请注意,由于别名模板在 for 循环中展开,因此创建的模板也将在其中发挥作用,因此可以很好地共享一个名称。
当然,在游戏设置中可能更正常的是使用更面向对象的方法(恕我直言,OO真正有意义的少数情况之一,但这是另一个讨论)。如果您为球类型设置了一个程序,则可以使用{.this: self.}
编译指示对其进行注释,以节省一些键入:
type
A = object
x: int
{.this: self.}
proc testproc(self: A) =
echo x # Here we can acces x without doing self.x
var t = A(x: 10)
t.testproc()
创建引用是有规则的,因此您可能需要使用不安全的指针进入Game
变量持有的内存,如下所示:
type
Vector[T] = object
x, y: T
RendererPtr = ref object
dummy: int
Ball = object
pos: Vector[float]
Game = ref object
renderer: RendererPtr
ball: array[10, Ball]
proc filledCircleRGBA(renderer: RendererPtr, x, y: int16,
a, b, c, d, e: int) =
discard
proc render(game: Game) =
# Render the balls
for ix in low(game.ball)..high(game.ball):
let ball: ptr Ball = addr game.ball[ix]
game.renderer.filledCircleRGBA(
int16(ball.pos.x), int16(ball.pos.y),
10, 100, 100, 100, 255)
请注意,let
仅适用于本地ball
别名,您仍然可以改变它指向的任何内容。减少类型的另一种方法可能是围绕filledCircleRGBA
编写一个包装器,该包装器接受要呈现的Ball
的Game
和索引:
proc filledCircleRGBA(renderer: RendererPtr, x, y: int16,
a, b, c, d, e: int) =
discard
proc filledCircleRGBA(game: Game, ballIndex: int,
a, b, c, d, e: int) =
filledCircleRGBA(game.renderer,
game.ball[ballIndex].pos.x.int16,
game.ball[ballIndex].pos.y.int16,
a, b, c, d, e)
proc render(game: Game) =
# Render the balls
for ix in low(game.ball)..high(game.ball):
game.filledCircleRGBA(ix, 10, 100, 100, 100, 255)
根据您的性能需求,您可以内联包装器proc
或将其转换为模板,以保证没有 proc 调用开销。