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

Experimental typeImports #8660

Closed
wants to merge 2 commits into from

Conversation

drslump
Copy link
Contributor

@drslump drslump commented Aug 16, 2018

Changes the behaviour of from m import T following the discussion at https://github.com/nim-lang/Nim/issues/8013

To be clear, since it's not exactly the same as discussed on the other issue, the behaviour implemented is:

  • Automatically include any symbol that references the imported type on its signature

That doesn't include generic types, basically because they are a tad more difficult to implement and I'm not knowledgeable enough to assert if it's something that should be allowed or not regardless of me being able to do the implementation. Comments about this more than welcome :)

##
case s.kind
of skProcKinds:
for tt in s.typ.sons.items:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: shall I avoid using iterators in the compiler code? maybe they are not as efficient as countup loops?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to avoid them, they are zero-cost.

doc/manual.rst Outdated
from colors import Color

# to string and proc imported
let pink = rgb(255, 192, 203)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this example shows that it's not enough the type is the return type of rgb.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I don't understand, could you rephrase?

Maybe it should demonstrate what's going on more explicitly let pink: Color = rgb(255, 192, 203)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that rgb should not be imported implicitly as the connection to Color is invisible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, yeah, you're right. I implemented it initially without taking into account the return type but it forced to include the factory function from m import T, initT which I found a pity... and then it occurred to me that one very simple way to teach this is:

Automatically include any symbol that references the imported type on its signature

I think it balances for the implicity on factories, specially since many times it's kind of obvious that they are creating the type.

##
## - Proc refers to the type on any of its input or output values.
##
## TODO: generics are not matched currently.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this should be done though. Requires a bit more code, will show you how to do that later.

@timotheecour
Copy link
Member

timotheecour commented Aug 17, 2018

IMO the feature should not work with regular import syntax:
from colors import Color
as a user could genuinely not want a particular method to be imported (even with UFCS)

instead, should be explicit (that way, doens't even have to be "experimental":
from colors import Color.* or, more explicit and flexible in case we want more control later:
import colors: withMethodsmethods(Color)

@Araq
Copy link
Member

Araq commented Aug 17, 2018

This feature exists to bring Nim closer to Python and I don't think we should introduce more syntax. And even if we did, the .experimental would remain, new features need to start in the experimental state, no matter how good they seem to be.

@drslump
Copy link
Contributor Author

drslump commented Aug 17, 2018

Implemented support for generics by using sigmatch.typeRel, it works but I couldn't say if there is any performance implications about the solution, I'm way in over my head to be honest :-)

@Araq
Copy link
Member

Araq commented Aug 20, 2018

  1. Use the typeRel mechanism for the first parameter only, not for return types or anything else.
  2. Yes this means from tables import Table, initTable but this is the better design. The feature exists because you argue it's not obvious where stuff "comes from" and it isn't obvious for rgb.

Otherwise every proc in e.g. regex deals with the Regex type and so import re is the same as from re import Regex. And arguably that's already the case for most modules out there, making this whole feature solve a non-issue, but hey, we already know that.

@drslump
Copy link
Contributor Author

drslump commented Aug 20, 2018

gotcha, so long, and thanks for all the fish

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

Successfully merging this pull request may close these issues.

3 participants