Importing a namespace in the same directory

General APL language issues

Importing a namespace in the same directory

Postby augustk on Wed Feb 06, 2019 4:47 pm

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: 19
Joined: Tue Feb 05, 2019 11:54 am

Re: Importing a namespace in the same directory

Postby Adam|Dyalog on Thu Feb 07, 2019 8:57 am

Instead of the comment (which is SALT's old syntax), the newer syntax uses a path relative to the current script's location:
      :Require file://[path]/file
Full documentation.

Note that such declarations must be placed before all other code:
      ⍝ comments are allowed here
:Require file://M.dyalog
:Namespace N

...

:EndNamespace
User avatar
Adam|Dyalog
 
Posts: 56
Joined: Thu Jun 25, 2015 1:13 pm

Re: Importing a namespace in the same directory

Postby ray on Thu Feb 07, 2019 4:12 pm

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
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 151
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK

Re: Importing a namespace in the same directory

Postby Adam|Dyalog on Thu Feb 07, 2019 4:48 pm

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.
User avatar
Adam|Dyalog
 
Posts: 56
Joined: Thu Jun 25, 2015 1:13 pm

Re: Importing a namespace in the same directory

Postby augustk on Mon Feb 11, 2019 10:06 am

Adam|Dyalog wrote:
      :Require file://M.dyalog
:Namespace N

...

:EndNamespace

I tried the above by I get a "Can't Fix" error:
line(0,9) : error AC0614: cannot read required file ":Require file://M.dyalog"
^
Complete: 1 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: 19
Joined: Tue Feb 05, 2019 11:54 am

Re: Importing a namespace in the same directory

Postby Adam|Dyalog on Mon Feb 11, 2019 10:48 am

We've tried reproducing the issue you are seeing on various versions, including 16.0.34631.0 32 Classic, but have not succeeded:
      '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.
User avatar
Adam|Dyalog
 
Posts: 56
Joined: Thu Jun 25, 2015 1:13 pm

Re: Importing a namespace in the same directory

Postby augustk on Mon Feb 11, 2019 11:45 am

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: 19
Joined: Tue Feb 05, 2019 11:54 am

Re: Importing a namespace in the same directory

Postby Adam|Dyalog on Mon Feb 11, 2019 12:22 pm

Short answer is: Use ⎕FIX instead of SALT.Load.

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).
User avatar
Adam|Dyalog
 
Posts: 56
Joined: Thu Jun 25, 2015 1:13 pm

Re: Importing a namespace in the same directory

Postby augustk on Mon Feb 11, 2019 3:37 pm

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: 19
Joined: Tue Feb 05, 2019 11:54 am

Re: Importing a namespace in the same directory

Postby Adam|Dyalog on Mon Feb 11, 2019 4:50 pm

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.
User avatar
Adam|Dyalog
 
Posts: 56
Joined: Thu Jun 25, 2015 1:13 pm

Next

Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest