Hello. I am trying to create a source file with variables containing paths to other sources files. My need is to rescue resources for the current source file, so my idea is to achieve it with something like this:
variablePathFromFileContainingResource="./folderContainingResourceSource/FileFromResourceSource.resource" from pathsFile import variablePathFromFileContainingResource
I tried to do it with this macro importing previously the source where it is located:
import macros
With which, although unsuccessfully, I have tried the syntax:
from pathsFile import getContentVariable(variablePathFromFileContainingResource)
Well, I could do it in the past with other languages, but in Nim it doesn't work. :-)
Any suggestions?
Best regards. Antonio F.S.
Hello. I think that would certainly be the case.
Thank you. Best regards. Antonio F.S.
Hello. After two days of struggling with the issue, it finally worked by declaring the path variable as a constant. :-)
Thank you very much though. Best regards. Antonio F.S.
Hello.
For some unknown reason, after modifying the source and executing it, the application worked correctly (hence my previous message), but then when recompiling again it does not support the constant with the path either. :-(
It seems that 'from` only supports the literal path name and not the ability to extract it from a variable or constant.
Best regards. Antonio F.S.
If I properly understood what you want - you can do that with this macro:
( might not be the best approach, it's literally my first ever macro =D )
import macros
macro importVar(filename, varname: static string): untyped =
result = newStmtList()
result.add nnkFromStmt.newTree(
newIdentNode(filename),
newIdentNode(varname)
)
const pathsFile = "./paths.nim"
const varname = "hello"
importVar(pathsFile, varname)
echo hello # Hello, Antonio!
# ./paths.nim
const hello* = "Hello, Antonio!"
Hello janAkali.
I have adapted your code to my needs and, indeed, dispensing with 'from' and 'import', it works fine. thank you very much! :-) :-)
In any case, how easy it would have been to have a command/symbol in Nim so that, as in other languages, placing it before the constant containing the path of the file that has the resource to use, it would have interpreted it as such.
For example, with the @ symbol: from @FilePathConstant import ResourceName
Explanation of janAkali's macro adaptation:
Source file macros.nim:
)
)
Source file paths.nim:
Source file in progress: import "folderLibs/paths". import "folderLibs/macros".
importVar(INPUT_ANIMAL_DATA, RESOURCE)
Best regards. Antonio F.S.
P.S.: I don't know how to get the code formatted correctly in this messaging system.
Click on the "Styling with RST is supported" that is under every input box to see how to format things. It uses the triple-backticks syntax that is universally supported at this point.
... how easy it would have been to have a command/symbol in Nim so that, as in other languages
const myResource = staticReadFile("foobar.txt") is pretty simple. Not sure why anybody would want special syntax for ordinary things.
Hi Araq.
Thank you for the clarification on formatting code.
Regarding your observation, I will tell you that in almost 62 years of this life that I am beginning to finish, I have learned that the individual mind is as especially diverse as the whole Universe can be. The ways of analysing and solving the same problem can be infinite, but if a single tool were to focus on all of them, it would be the best possible unification in any programming language. :-)
Best regards. Antonio F.S.
Hello. I post below for further clarification, the summary of this thread:
Best regards. Antonio F.S.
#[
Author: janAkali // Forum: -> https://forum.nim-lang.org/
Date: 28/11/2024
Description: gets a resource from a Nim source file.
Example:
Source file macros.nim:
import macros
macro importVar*(filename, varname: static string): untyped =
result = newStmtList(
nnkFromStmt.newTree(
newIdentNode(filename),
newIdentNode(varname)
)
)
Source file paths.nim:
const
SOURCE_FILE* = "/home/afs3/Nim/Project/Views/SourceResourceFile".
RESOURCE* =
"resourceFromSourceFileWithResource".
Current source file:
import "folderLibs/paths".
import "folderLibs/macros".
importVar(SOURCE_FILE, RESOURCE)
]#