Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{.borrow:..} not working with distinct generics #14449

Closed
mratsim opened this issue May 25, 2020 · 6 comments · Fixed by #18848
Closed

{.borrow:..} not working with distinct generics #14449

mratsim opened this issue May 25, 2020 · 6 comments · Fixed by #18848

Comments

@mratsim
Copy link
Collaborator

mratsim commented May 25, 2020

The following fails to compile

type Foo[T] = object
  foo: T

type Bar[T] {.borrow:`.`.} = distinct Foo[T]
@Vindaar
Copy link
Contributor

Vindaar commented Jul 9, 2020

Yep, just noticed that too.

@boyanl
Copy link

boyanl commented Aug 23, 2020

Sorry for the newbie question, but is there a way to work around this, apart from re-declaring accessors for the distinct type? (it's not too bad, but I'm wondering if there's something shorter)

@mratsim
Copy link
Collaborator Author

mratsim commented Aug 24, 2020

Working around type bugs is not a newbie question but an art. I use though mutable parameters requires some tricks with casts to raw addresses:

https://github.com/mratsim/trace-of-radiance/blob/99f7d85dec857f0efa0c9b0eafd339871599c0e1/trace_of_radiance/scenes_animated.nim#L71-L88

# workarounds https://github.com/nim-lang/Nim/issues/14440
# so ugly ...
func `+=`(a: var Distance, b: Distance) {.inline.}=
  cast[var float64](a.addr) += b.float64
func `-=`(a: var Velocity, b: Velocity) {.inline.}=
  cast[var float64](a.addr) -= b.float64

func `+=`(a: var ATime, b: ATime) {.inline.}=
  cast[var float32](a.addr) += b.float32
template `<`(a, b: ATime): bool =
  a.float32 < b.float32

template `*`(accel: Acceleration, dt: ATime): Velocity =
  Velocity(accel.float64 * dt.float64)
template `*`(v: Velocity, dt: ATime): Distance =
  Distance(v.float64 * dt.float64)
template `*`(k: float64, v: Velocity): Velocity =
  Velocity(k * v.float64)

@boyanl
Copy link

boyanl commented Aug 24, 2020

Thanks, but unless I'm missing a subtler point, it seem like it's a different issue?
I'll just do without the generic type for now, it's easy enough

@mratsim
Copy link
Collaborator Author

mratsim commented Aug 24, 2020

Ah right it was #14440, my bad. I created many distinct issues at that time.

@ringabout
Copy link
Member

Related: #6039

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants