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
mixin value causes typechecker to fail #7310
Comments
I am sure something is not right, but what does it even mean to mixin a string? |
I think the compiler is right (though the error message is bad). |
@andreaferretti I am trying to do something like environment variables in unix. Or dynamic scoping like in emacs lisp. I just give you an example of this use case so you might understand it a bit better. Take the random module for an example with it's random number generator. You want a simple to use let a = rand() # default global random number generator
let b = rand() # default global random number generator
let randomenv = #[ init random number generator ]#
let c = rand() # now randomenv shadows the default random number generator
let d = rand() # again random number from randomenv A second example where this might be useful is for memory allocation. Normally when you have a library that you call and you know it allocats a lot of junk temporary memory the garbage collector would be required to collect it all. With this pattern it would be possible to redirect the memory allocation to a memory pool for the library call and then just reset the memory pool. let memoryenv = createMemoryPool(size = megabyte(32))
var accu = 0
for i in 0 ..< 1000:
let myResult = libraryCallWithLotsOfTemporaryMemoryAllocation()
memoryenv.reset # Everything that still holds a pointer into the data of `memoryenv` is invalid
accu += myResult @Araq would there be something like look up in instanciation scope? Because when do the same pattern with just macros, I have no problems: import macros
let theEnvironment = "default-env"
proc foobar_internal(env: string; arg: int): void =
echo "form ", env, " called foobar with arg: ", arg
macro foobar(arg: int): typed =
result = newCall(bindSym"foobar_internal", ident"theEnvironment", arg)
foobar(1)
proc main(): void =
let theEnvironment = "local-env"
foobar(2)
main()
# output:
#
# form default-env called foobar with arg: 1
# form local-env called foobar with arg: 2 |
@krux02 I understand what you want to do, but what Scala has implicit parameters for that - they are both a blessing and a curse |
I know implicit parameters in Scala. Even though they bind by Type and not by name, they would do the trick here. |
Use a .dirty template and maybe |
Yes a dirty template works. Still I don't like the dirty template, because it is dirty 🙃. Yea I mean it binds all identifiers in instanciation scope and I can't decide which identifiers should come from that scope. If there would be an instanciation scope mixin/bind, then also other use cases of the |
My suggestion would be to avoid dynamic variables like the plague, but i'ts your choice... :-D |
@andreaferretti yea are probably right. Though they are useful. And this is probably not the way to implement them. |
@Araq I just tried out the dirty pragma and it does not work: # filename: "mymodule.nim"
let theEnvironment = "default-env"
proc foobar_internal(env: string; arg: int): void =
echo "form ", env, " called foobar with arg: ", arg
template foobar*(arg: int): void {.dirty.} =
bind foobar_internal
foobar_internal(theEnvironment, arg)
# filename: "scratch.nim"
import mymodule
foobar(1)
proc main(): void =
let theEnvironment = "local-env"
foobar(2)
main() error:
when I leave out the bind statement it has this error: |
Huh, strange... |
I don't think it's strange at all. The dirty praga does resolve symbols at instanciation context. The problem is foobar_iternal is not exported and therefore in instanciation context it can't be resolved. |
This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions. |
compilation error:
In the error it tells me that the first argument is of type string, but then later it tells it is of type
None
. That cannot be right.The text was updated successfully, but these errors were encountered: