Skip to content

Instantly share code, notes, and snippets.

@dom96

dom96/fixed.nim Secret

Created April 4, 2018 20:46
Show Gist options
  • Save dom96/253997643d18b961f79ddc5336d9e139 to your computer and use it in GitHub Desktop.
Save dom96/253997643d18b961f79ddc5336d9e139 to your computer and use it in GitHub Desktop.
type
# ...
Vector*[T] = object
x*, y*: T
Animatable* = ref object of RootObj
Ball* = ref object of Animatable
pos*: Vector[float]
method clone*(anim: Animatable): Animatable {.base.} =
raise
method badclone*(anim: Animatable): Animatable {.base.} =
raise
# this makes a bad (incomplete) copy
method badclone*(ball: Ball): Animatable =
result.new
result.deepCopy(ball)
# This works
method clone*(ball: Ball): Animatable =
var ball2 = new Ball
ball2.deepCopy(ball)
result = ball2
var
ball = Ball(pos: Vector[float](x: 111, y: 999))
goodball = ball.clone
badball = ball.badclone
echo repr ball
echo repr goodball
echo repr badball
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment