nim-lang / RFCs

A repository for your Nim proposals.
136 stars 26 forks source link

Procedures from system should have a lower priority then procedures from outside system. #268

Open solo989 opened 3 years ago

solo989 commented 3 years ago

Procedures created outside of system should have higher priority then procedures created inside system. If an ambiguity error would be thrown the procedure created outside of system would be chosen over any procedures defined in system.

Any additions to the standard library shouldn't cause conflicts with procedures defined in user code. This would be minimized if user defined procedures took priority.

I noticed this problem when I recently switched from the standard gc to arc and my overwrite of repr(NimNode) was causing ambiguity errors. I also tried to overwrite $(type) but ultimately made by own toStr procedure for types.

This can be worked around by putting import system except repr,$ at the top of your file but unfortunately that also drops a lot of other useful procedures. Also requiring users of your code to do this as well in the case of generics would be tedious. You could also qualify imports with myModule.repr.

Araq commented 3 years ago

Other solutions:

But what you propose makes sense too.

metagn commented 3 years ago

Could we prioritize based on import order, with system/other default imports being at the lowest priority? If you use from import then only the symbols you import would have priority.

disruptek commented 3 years ago

I'd prefer that users special-cased this stuff rather than having Nim always behave inconsistently merely for convenience of lazy users.

timotheecour commented 3 years ago

This can be worked around by putting import system except repr,$ at the top of your file but unfortunately that also drops a lot of other useful procedures.

We should instead allow specifying overloads in import/export syntax, eg:

import foo except `$`(a: Bar)
from foo import `$`(a: Bar1), `$`(a: Bar2)
export `$(a: Bar3)`

for system this could work as follows, since string + friends is not yet defined:

from system import nil
import system except `$`(a: system.string)

This doesn't special case system nor introduce a priority amongst imports, which sounds like a can of worms unless you can describe exactly how that would work.

Araq commented 3 years ago

While that sounds useful it's not required either. This works:


import system except repr
echo system.repr(12)
timotheecour commented 3 years ago

it's not required either.

OP also mentioned $:

I also tried to overwrite $(type) but ultimately made by own toStr procedure for types. putting import system except repr,$ at the top of your file but unfortunately that also drops a lot of other useful procedures

metagn commented 3 years ago

We should instead allow specifying overloads in import/export syntax

Can we have a separate RFC for this? I think this could be very useful and I'd like to hear separate opinions about it.

github-actions[bot] commented 8 months ago

This RFC is stale because it has been open for 1095 days with no activity. Contribute a fix or comment on the issue, or it will be closed in 30 days.