Function as a display form of a namespace
6 posts
• Page 1 of 1
Function as a display form of a namespace
Is it possible to use a function as a display form of a namespace, to make it showing some meaningful representation of current content of the namespace?
-
OSRK - Posts: 12
- Joined: Tue Dec 27, 2016 8:48 am
Re: Function as a display form of a namespace
From your question it is not totally obvious in which context you need this fn...i.e. is it for your own interactive explorations of the ws, are u thinking of a fn to use in your app etc.?
Sorry if this sounds too basic, but you've already found WS Explorer for this, right?
The cases where I wanted a quick way to get info about the cntents of a ns, I was concerned with the variables in the ns and their values...and a nice shortcut to do this is by just doing "⎕JSON ns". This gives you a JSON-Representation of the contents of the ns, this is nice and easy if you have a few text-variables or simple numerics...but if there a many large matrices in the ns, it looses its appeal ;-)
If you are interested in fns, vars etc., the user-command ]map might be interesting. Just do a "]?map" for some doc on its usage and "]map ns" to apply it on a ns.
Sorry if this sounds too basic, but you've already found WS Explorer for this, right?
The cases where I wanted a quick way to get info about the cntents of a ns, I was concerned with the variables in the ns and their values...and a nice shortcut to do this is by just doing "⎕JSON ns". This gives you a JSON-Representation of the contents of the ns, this is nice and easy if you have a few text-variables or simple numerics...but if there a many large matrices in the ns, it looses its appeal ;-)
If you are interested in fns, vars etc., the user-command ]map might be interesting. Just do a "]?map" for some doc on its usage and "]map ns" to apply it on a ns.
- Michael|Dyalog
- Posts: 23
- Joined: Thu Mar 02, 2017 4:29 pm
Re: Function as a display form of a namespace
I'm guessing your questions is related to the fact that ⎕DF only allows you to set an array as the "display form" for a namespace. In version 17.0 you will be able to get a little closer to what you want with the extension to trigger functions to allow them to trigger on any change in a namespace:
- Code: Select all
:Namespace MyNs
x←42
∇ setdisplay
:Implements Trigger *
⎕DF 'x is now: ',⍕x
∇
:EndNamespace
MyNs.x←99
MyNs
x is now: 99
-
Morten|Dyalog - Posts: 451
- Joined: Tue Sep 09, 2008 3:52 pm
Re: Function as a display form of a namespace
Michael|Dyalog, the idea is to watch a "value" (a representation, defined by myself) of a namespace while tracing. I want to press Shift+Enter on a namespace, and see its "value". Then continue tracing, and watch how it changes.
WS Explorer is not suitable for this, as it requires a lot of clicks, and allows to watch one property only.
When I try "]map", I get VALUE ERROR:
Morten|Dyalog, this looks promising, thank you. Although, I would expect the representation calculation to be called only when the representation is actually requested, instead of being updated on every change of the namespace, to not to compromise performance. But even this solution could be enabled in dev mode only, not affecting real-world performance.
WS Explorer is not suitable for this, as it requires a lot of clicks, and allows to watch one property only.
When I try "]map", I get VALUE ERROR:
Dyalog APL/W-64 Version 16.0.31811
Unicode Edition
]?map
VALUE ERROR
ns←⎕NS''
ns.foo←'bar'
ns.baz←42
]map ns
VALUE ERROR
Morten|Dyalog, this looks promising, thank you. Although, I would expect the representation calculation to be called only when the representation is actually requested, instead of being updated on every change of the namespace, to not to compromise performance. But even this solution could be enabled in dev mode only, not affecting real-world performance.
-
OSRK - Posts: 12
- Joined: Tue Dec 27, 2016 8:48 am
Re: Function as a display form of a namespace
Here is another idea: Make your namespace into a class (and make everything "public shared" so it acts like a normal namespace). You can then define a "default property". The result of the property's getter function will be become the data for squad and bracket indexing instead of the ref itself:
Try it online!
↑⎕SRC cl
:Class cl
:field public shared x←42
:field public shared foo←'bar'
:field public shared baz←42
:property default df
:access public shared
∇ df←Get
df←↑('x is 'x)('foo is 'foo)('baz is 'baz)
∇
:endproperty
:EndClass
⌷cl
┌───────┬───┐
│x is │42 │
├───────┼───┤
│foo is │bar│
├───────┼───┤
│baz is │42 │
└───────┴───┘
cl.x←99
cl[;2]
┌──┬───┬──┐
│99│bar│42│
└──┴───┴──┘
Try it online!
-
Adam|Dyalog - Posts: 134
- Joined: Thu Jun 25, 2015 1:13 pm
Re: Function as a display form of a namespace
Regarding your (Morten's) namespace example, I believe the trigger code works already in version 16, unless I'm missing something. See Version 16 Release Notes, p. 26.
As a tweak of your v. 17 code, I used this code in 16, simply adding a right argument I don't reference (the trigger is simply and quietly ignored if the right argument is missing, at least in the Mac version):
The display form for MyNs will be updated when ANY variable in MyNs is changed...
"Hovering" over the name will show the (updated) ⎕DF display information.
(Bug: If you expunge a variable-- MyNs.⎕EX 'c' --, the TRIGGER function won't be called.)
The Class "trick" also works perfectly and would allow the same trigger code, as well.
As a tweak of your v. 17 code, I used this code in 16, simply adding a right argument I don't reference (the trigger is simply and quietly ignored if the right argument is missing, at least in the Mac version):
- Code: Select all
⍝ Place in MyNs
∇setdisplay _ignore_me
:Implements Trigger *
⎕DF ⎕FMT↑{
(⍵,' is now:')(⎕OR ⍵)
}¨↓⎕NL 2
∇
)cs
#
MyNs.(a b c)←10 'cats' (⍳10)
MyNs
a is now: 10
b is now: cats
c is now: 1 2 3 4 5 6 7 8 9 10
The display form for MyNs will be updated when ANY variable in MyNs is changed...
"Hovering" over the name will show the (updated) ⎕DF display information.
(Bug: If you expunge a variable-- MyNs.⎕EX 'c' --, the TRIGGER function won't be called.)
The Class "trick" also works perfectly and would allow the same trigger code, as well.
- petermsiegel
- Posts: 141
- Joined: Thu Nov 11, 2010 11:04 pm
6 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group