Importing a namespace in the same directory
13 posts
• Page 1 of 2 • 1, 2
Importing a namespace in the same directory
Given a namespace N which uses features from a namespace M which is stored in the same directory as N, is there a way to import M without having to hard-code its absolute path? If not, is there a way to get the path to the current namespace? I tried simply leaving out the path but that didn't work.
- Code: Select all
:Namespace N
⍝∇:require M
...
:EndNamespace
- augustk
- Posts: 27
- Joined: Tue Feb 05, 2019 11:54 am
Re: Importing a namespace in the same directory
Instead of the comment (which is SALT's old syntax), the newer syntax uses a path relative to the current script's location:
Note that such declarations must be placed before all other code:
:Require file://[path]/fileFull documentation.
Note that such declarations must be placed before all other code:
⍝ comments are allowed here
:Require file://M.dyalog
:Namespace N
...
:EndNamespace
-
Adam|Dyalog - Posts: 135
- Joined: Thu Jun 25, 2015 1:13 pm
Re: Importing a namespace in the same directory
Can anyone explain for me the difference between the ":Require" and the ":Include" keywords, they appear to be similar? I have only ever use the ":Include"
Thanks
Ray
Thanks
Ray
Ray Cannon
Please excuse any smelling pisstakes.
Please excuse any smelling pisstakes.
-
ray - Posts: 221
- Joined: Wed Feb 24, 2010 12:24 am
- Location: Blackwater, Camberley. UK
Re: Importing a namespace in the same directory
Hi Ray,
":Require <file>" ⎕FIXes <file> before the current script is processed, but nothing is imported anywhere.
":Include <ns>" makes all functions in <ns> callable in the current namespace as if they were members here.
I hope this clarifies matters.
":Require <file>" ⎕FIXes <file> before the current script is processed, but nothing is imported anywhere.
":Include <ns>" makes all functions in <ns> callable in the current namespace as if they were members here.
I hope this clarifies matters.
-
Adam|Dyalog - Posts: 135
- Joined: Thu Jun 25, 2015 1:13 pm
Re: Importing a namespace in the same directory
Adam|Dyalog wrote::Require file://M.dyalog
:Namespace N
...
:EndNamespace
I tried the above by I get a "Can't Fix" error:
Here are my two test files (which are located in the same directory):
M.dyalog
- Code: Select all
:Namespace M
∇ R ← F Y
R ← Y + 1
∇
:EndNamespace
N.dyalog
- Code: Select all
:Require file://M.dyalog
:Namespace N
∇ Run
#.M.F 1
∇
:EndNamespace
Any clues? I use Dyalog version 16.0.34631.0 32 Classic.
- augustk
- Posts: 27
- Joined: Tue Feb 05, 2019 11:54 am
Re: Importing a namespace in the same directory
We've tried reproducing the issue you are seeing on various versions, including 16.0.34631.0 32 Classic, but have not succeeded:
As expected. Try it online!
If you can post the exact code that causes the issue, or alternatively modify the "Try it online" to reproduce the issue, then we'll have a look.
'M.dyalog'⎕NPUT⍨⊂':Namespace M' '∇R←F Y' 'R←Y+1' '∇' ':EndNamespace'
'N.dyalog'⎕NPUT⍨⊂':Require file://M.dyalog ' ':Namespace N' '∇Run' '#.M.F 1' '∇' ':EndNamespace'
⎕FIX'file://N.dyalog'
N.Run
2
As expected. Try it online!
If you can post the exact code that causes the issue, or alternatively modify the "Try it online" to reproduce the issue, then we'll have a look.
-
Adam|Dyalog - Posts: 135
- Joined: Thu Jun 25, 2015 1:13 pm
Re: Importing a namespace in the same directory
Adam|Dyalog wrote:If you can post the exact code that causes the issue, or alternatively modify the "Try it online" to reproduce the issue, then we'll have a look.
I posted the exact code in my last message. I load N with
- Code: Select all
⎕SE.SALT.Load 'C:\Users\august.karlstrom\Documents\progtest\apl\N'
If I then ]CD to the directory of N I can fix the namespace (with the initial :Require line) using Ctrl-down. This indicates that the :Require path is relative to the current directory in the session rather than the directory of the saved file. So, when I press Ctrl-down the namespace is fixed, however, the file is not saved. Does SALT require the file to start with :Namespace? I also get a value error when I invoke N.Run.
- augustk
- Posts: 27
- Joined: Tue Feb 05, 2019 11:54 am
Re: Importing a namespace in the same directory
Short answer is: Use ⎕FIX instead of SALT.Load.
Longer answer: The documentation should maybe be more clear on this:
What is happening is that SALT maintains its own association of files and workspace content, while ⎕FIX use built-in functionality in the interpreter. So if you use ⎕FIX'file://…' then the namespace is considered coming from a "script file" while if you use SALT.Load it is considered "a workspace script" (that is, a script contained in the workspace rather than contained in a separate plain text file).
Longer answer: The documentation should maybe be more clear on this:
If no path is specified, the path is taken to be relative to the current script file or, if in a workspace script, the current working directory. Note that a leading './' or '.\' in path is not allowed, to avoid any potential confusion with "current directory".
:Require is a directive to the Editor (more specifically, to the internal mechanism that fixes a script as an object in the workspace)
What is happening is that SALT maintains its own association of files and workspace content, while ⎕FIX use built-in functionality in the interpreter. So if you use ⎕FIX'file://…' then the namespace is considered coming from a "script file" while if you use SALT.Load it is considered "a workspace script" (that is, a script contained in the workspace rather than contained in a separate plain text file).
-
Adam|Dyalog - Posts: 135
- Joined: Thu Jun 25, 2015 1:13 pm
Re: Importing a namespace in the same directory
Adam|Dyalog wrote:Short answer is: Use ⎕FIX instead of SALT.Load.
Thanks, using ⎕FIX works like a charm. By the way, is there a way to import M so that using features from it in N does not require the leading # in the designator, e.g. so I can write M.F instead of #.M.F?
- augustk
- Posts: 27
- Joined: Tue Feb 05, 2019 11:54 am
Re: Importing a namespace in the same directory
I'm happy that worked!
If your N namespace script has a line saying M←#.M then all subsequent references to M will go to M in the root of the namespace. Try it online!
However, it may be of interest to use M←##.M instead which goes to the N's sibling M wherever N is.
If your N namespace script has a line saying M←#.M then all subsequent references to M will go to M in the root of the namespace. Try it online!
However, it may be of interest to use M←##.M instead which goes to the N's sibling M wherever N is.
-
Adam|Dyalog - Posts: 135
- Joined: Thu Jun 25, 2015 1:13 pm
13 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group