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

Flexible import syntax #10527

Closed
wants to merge 2 commits into from
Closed

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Feb 2, 2019

Flexible import syntax, allowing to import a path (or multiple paths) computed at compile time from an expression, instead of standard import which requires a litteral.

  # import from a path expression:
  const path = getSomePath()
  importInterp path

   # same as above:
  importInterp getSomePath()

   # same, but with alias syntax (`pathAlias` is a symbol, not a string):
  importInterp path, pathAlias

   # same, but with partial import:
  importInterp path: [foo1, foo2]

   # import from multiple paths at once (can be any compile time expression):
  importInterp ["os", "strutils"]

   # same but requiring qualified imports:
  importInterp ["os", "strutils"]: nil
# import relative to root of current nimble package:
importInterp getCurrentPkgDir() / "foo/bar"

# provides an alternative  to current approach where you have to know how many `..` to add:
import "../../foo/bar"

@timotheecour timotheecour changed the title importInterp: import a path from an expr evaluated at compile time Flexible import syntax Feb 2, 2019
@GULPF
Copy link
Member

GULPF commented Feb 2, 2019

additional example use case, using getCurrentPkgDir from #10530:

Can't this be done by simply adding the package root to the search path, or does that have some drawback that I'm missing? Prefixing every import from the local package with getCurrentPkgDir is ugly.

I don't think an additional syntax for imports belongs in the stdlib. This would be better as a nimble package.

@timotheecour
Copy link
Member Author

timotheecour commented Feb 2, 2019

Can't this be done by simply adding the package root to the search path

import foo could still pickup a path outside of the current nimble package; getCurrentPkgDir prevents that

Prefixing every import from the local package with getCurrentPkgDir is ugly

noone's forcing you to, and it's not needed for sibling files, eg:

# this is fine:
import ./foo/bar
# ditto
import "." / foo/bar

# this is awkward and error prone: 
import ../../../foo/bar

# this one can pickup the wrong file depending on what's in nimble path (you may not want to do global changes to nimble eg in case other uses processes rely on a given pkg configuration)
import regex/foo/bar

# this one can be used in this case instead (but noone's forcing you to)
import getCurrentPkgDir() / "foo/bar"

# note: there are other use cases, eg:
const binDir = getCurrentPkgDir() / "bin" # do something with binDir

@Araq
Copy link
Member

Araq commented Feb 2, 2019

provides an alternative to current approach where you have to know how many .. to add:
import "../../foo/bar"

Yes, you have to "know" how many '..' to add and if your directory hierarchy is not a deeply nested mess, it's not hard to know. And if it is a deeply nested mess, maybe you should use fewer subdirectories.

@narimiran
Copy link
Member

Opinion, not an issue.

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

Successfully merging this pull request may close these issues.

4 participants