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

Can't overload = for a generic. #8973

Open
kayabaNerve opened this issue Sep 15, 2018 · 5 comments
Open

Can't overload = for a generic. #8973

kayabaNerve opened this issue Sep 15, 2018 · 5 comments

Comments

@kayabaNerve
Copy link
Collaborator

For a Generic defined as:

type Generic*[T] = object
    value: T

There are four potential definitions of =.

proc `=`(x: var Generic, y: Generic) #Doesn't match the needed signature.

proc `=`(x: var Generic[int], y: Generic[int]) #Doesn't match the needed signature and violates the purpose of Generics.

proc `=`(x: var Generic[X], y: Generic[X]) #X (not T as T is Generic[X]) is undefined.

proc `=`[T](x: var Generic[T], y: Generic[T]) #Compiles, but is never run.

Is this a feature that has yet to be implemented/is broken or will Nim not support Generic = overloading?

@Araq
Copy link
Member

Araq commented Sep 15, 2018

proc =[T](x: var Generic[T], y: Generic[T]) #Compiles, but is never run.

That's currently the only supported syntax and it is invoked, I have plenty of tests that prove it. :P

@mratsim
Copy link
Collaborator

mratsim commented Sep 15, 2018

I wouldn't trust assignment overloading.

Quoting @Araq in #6348 (comment)

Assignment overloading is unusable for everything right now.

My experiments to introduce a custom copy-on-write also failed utterly due to it: #6786

@kayabaNerve
Copy link
Collaborator Author

I'm obviously not someone who knows the Nim compiler as well as @Araq, but it hasn't been invoked on my tests in devel.

My guess is [T] is being defined as Generic[X] so it has to match Generic[Generic[X]], which it doesn't. For whatever reason it isn't working, do you have an test that explicitly tests overloading for generics?

@kayabaNerve
Copy link
Collaborator Author

Here's the exact code I'm testing with a devel compiler from November 11th.

type Generic[T] = object
    value: T

proc `=`[T](x: var Generic[T], y: Generic[T]) = #Compiles, but is never run.
    echo 1

var t: Generic[int]
t = Generic[int](value: 5)
echo t.value

1 is never printed; the value is set to 5.

@kayabaNerve
Copy link
Collaborator Author

https://pastebin.com/fnndzshY

Those are three different methods of Generic creation. One uses alloc inside a function. One doesn't alloc anything. One uses alloc outside of a function. Only the one that uses alloc inside a function triggers.

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

No branches or pull requests

3 participants