Quantcast

TIP #471: Add [info linkedname] Introspection Command

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

TIP #471: Add [info linkedname] Introspection Command

Mathieu Lafon

 TIP #471: ADD [INFO LINKEDNAME] INTROSPECTION COMMAND
=======================================================
 Version:      $Revision: 1.1 $
 Author:       Mathieu Lafon <mlafon_at_gmail.com>
 State:        Draft
 Type:         Project
 Tcl-Version:  8.7
 Vote:         Pending
 Created:      Friday, 05 May 2017
 URL:          http://purl.org/tcl/tip/471.html
 WebEdit:      http://purl.org/tcl/tip/edit/471
 Post-History:

-------------------------------------------------------------------------

 ABSTRACT
==========

 This TIP proposes to improve link variable introspection by providing a
 new *info linkedname* command.

 RATIONALE
===========

 This TIP is related to discussions about [TIP #457] and the *-upvar*
 extended argument specifier. Adding an intropsection command to get the
 name of the variable linked to is more Tcl-ish than automatically
 adding a local variable with the linked name. The proposed command is
 not restricted to [TIP #457] usage as this can also be used for a link
 variable created by other means, using the *upvar* command for example.

 SPECIFICATION OF THE PROPOSED CHANGE
======================================

 There should be a new subcommand of *info* created with the following
 syntax:

       *info linkedname* /varname/

 The /varname/ should be the name of a variable that has been linked to
 another variable (e.g., with *upvar*, *global*, *variable* or
 *namespace upvar*), and the result of the command will be the name of
 the variable linked to.

 REFERENCE IMPLEMENTATION
==========================

 The reference implementation is available in the info-linkedname
 [<URL:http://core.tcl.tk/tcl/timeline?r=info-linkedname>] branch.

 The code is licensed under the BSD license.

 IMPLEMENTATION NOTES
----------------------

 Depending on the linked variable, the name is found using different
 methods:

     * The name of a variable present in a hash table (globals, local
       variables created at runtime, ...) is retrieved using the hash
       key;

     * The name of an array element is built using the name of the array
       and the index name, retrieved using the hash key. A new field is
       added to the TclVarHashTable sructure to access the related array
       variable from the array element;

     * The name of a compiled local variable is searched in current or
       upper call frames.

 COPYRIGHT
===========

 This document has been placed in the public domain.

-------------------------------------------------------------------------

 TIP AutoGenerator - written by Donal K. Fellows

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Tcl-Core mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/tcl-core
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TIP #471: Add [info linkedname] Introspection Command

Harald Oehlmann
Mathieu,
thank you for the great tip !

Am 08.05.2017 um 16:35 schrieb Mathieu Lafon:
>
>  TIP #471: ADD [INFO LINKEDNAME] INTROSPECTION COMMAND

>  SPECIFICATION OF THE PROPOSED CHANGE
> ======================================
>
>  There should be a new subcommand of *info* created with the following
>  syntax:
>
>        *info linkedname* /varname/
>
>  The /varname/ should be the name of a variable that has been linked to
>  another variable (e.g., with *upvar*, *global*, *variable* or
>  *namespace upvar*), and the result of the command will be the name of
>  the variable linked to.

Would it be wise to have a section in the TIP what happens, if:
a) the variable is not defined at all
b) the variable is an array (only mentioned in the implementation part)
c) the variable is not linked, e.g. local, global, namespace global ?
    May "info linkname" be used to check if a variable is linked in
    general?
d) it is used up the stack ?
    proc f1 aName {upvar 1 $aName a; f2 a}
    proc f2 bName {upvar 1 $bName b; puts [uplevel 1 [info linkname b]}
    set a 1 ; f1 a
e) it is a link on a link ?
    set a 1
    upvar #0 a b
    upvar #0 b c
    info linkname c
    -> a or b ?

Sorry, only questions which came to my mind...
Thank you,
Harald

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Tcl-Core mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/tcl-core
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TIP #471: Add [info linkedname] Introspection Command

Brian Griffin-4
In reply to this post by Mathieu Lafon

On May 8, 2017, at 7:35 AM, Mathieu Lafon <[hidden email]> wrote:


TIP #471: ADD [INFO LINKEDNAME] INTROSPECTION COMMAND
=======================================================
Version:      $Revision: 1.1 $
Author:       Mathieu Lafon <mlafon_at_gmail.com>
State:        Draft
Type:         Project
Tcl-Version:  8.7
Vote:         Pending
Created:      Friday, 05 May 2017
URL:          http://purl.org/tcl/tip/471.html
WebEdit:      http://purl.org/tcl/tip/edit/471
Post-History:

-------------------------------------------------------------------------

ABSTRACT
==========

This TIP proposes to improve link variable introspection by providing a
new *info linkedname* command.

RATIONALE
===========

This TIP is related to discussions about [TIP #457] and the *-upvar*
extended argument specifier. Adding an intropsection command to get the
name of the variable linked to is more Tcl-ish than automatically
adding a local variable with the linked name. The proposed command is
not restricted to [TIP #457] usage as this can also be used for a link
variable created by other means, using the *upvar* command for example.

SPECIFICATION OF THE PROPOSED CHANGE
======================================

There should be a new subcommand of *info* created with the following
syntax:

      *info linkedname* /varname/

The /varname/ should be the name of a variable that has been linked to
another variable (e.g., with *upvar*, *global*, *variable* or
*namespace upvar*), and the result of the command will be the name of
the variable linked to.

I think this is a great idea.  I would, however, suggest using the subcommand "upvar" instead of creating a new keyword which doesn't immediately relate to anything in Tcl.

    *info upvar* /varname/

And, in the spirit of introspection, why not allow patterns:

    *info upvars* /pattern/

This would list all the variables that where created via upvar semantics or, return a dict of name linkedname pairs.

-Brian


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Tcl-Core mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/tcl-core
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TIP #471: Add [info linkedname] Introspection Command

Mathieu Lafon
Hello,

On Mon, May 8, 2017 at 5:54 PM, Brian Griffin <[hidden email]> wrote:

> I think this is a great idea.  I would, however, suggest using the
> subcommand "upvar" instead of creating a new keyword which doesn't
> immediately relate to anything in Tcl.
>
>     *info upvar* /varname/
>
> And, in the spirit of introspection, why not allow patterns:
>
>     *info upvars* /pattern/
>
> This would list all the variables that where created via upvar semantics or,
> return a dict of name linkedname pairs.

That's a good suggestion, I will update the TIP and implementation to
use these subcommands.

On Mon, May 8, 2017 at 5:22 PM, Harald Oehlmann
<[hidden email]> wrote:
> Would it be wise to have a section in the TIP what happens, if:

Yes, I will add a dedicated section with examples.

> a) the variable is not defined at all

An error is returned.
% info linkedname foo
can't access "foo": no such variable

> b) the variable is an array (only mentioned in the implementation part)

I assume you want to know what happens if the linked variable is an
array element.
% upvar 0 a(1) upA
% info linkedname upA
a(1)

> c) the variable is not linked, e.g. local, global, namespace global ?
>     May "info linkname" be used to check if a variable is linked in
>     general?

If varname is not a link, an error is returned
% set i 0
0
% info linkedname i
can't access "i": variable isn't a link

> d) it is used up the stack ?
>     proc f1 aName {upvar 1 $aName a; f2 a}
>     proc f2 bName {upvar 1 $bName b; puts [uplevel 1 [info linkname b]}
>     set a 1 ; f1 a

I'm not sure to understand your point, do you mean : puts [uplevel 1
{info linkedname b}] ? In that case, b is not known in upper frame,
you will get an error.

> e) it is a link on a link ?
>     set a 1
>     upvar #0 a b
>     upvar #0 b c
>     info linkname c
>     -> a or b ?

This will return "a" *but* not because intermediate links are
followed, but because c will internally be directly linked to a. [info
linkedname] will not follow intermediate links if present.

-- Mathieu

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Tcl-Core mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/tcl-core
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TIP #471: Add [info linkedname] Introspection Command

Andreas Leitgeb
Mathieu Lafon <[hidden email]> wrote:
> > e) it is a link on a link ?
> >     set a 1
> >     upvar #0 a b
> >     upvar #0 b c
> >     info linkname c
> >     -> a or b ?
> This will return "a" *but* not because intermediate links are
> followed, but because c will internally be directly linked to a.
> [info linkedname] will not follow intermediate links if present.

That is a serious problem, imho.

The point of retrieving the name of a variable behind a link is to
know the name of the variable from caller's PoV - the name which was
used with (-)upvar and the known level to result in the link.

The name of the original variable may be useful, too, but *only* if we
then also know the (relative or absolute) *level* at which the original
variable existed.  But even then it is just not a substitute for the case
at hand, namely to capture what the caller gave for an "-upvar"-tagged
parameter.

Would it be possible to capture the immediate "parent" in the linkvar
structure at the point where upvar/variable/global or an -upvar is
handled?


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Tcl-Core mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/tcl-core
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TIP #471: Add [info linkedname] Introspection Command

Mathieu Lafon
Hello Andreas,

On Wed, May 17, 2017 at 2:37 PM, Andreas Leitgeb <[hidden email]> wrote:

> Mathieu Lafon <[hidden email]> wrote:
>> > e) it is a link on a link ?
>> >     set a 1
>> >     upvar #0 a b
>> >     upvar #0 b c
>> >     info linkname c
>> >     -> a or b ?
>> This will return "a" *but* not because intermediate links are
>> followed, but because c will internally be directly linked to a.
>> [info linkedname] will not follow intermediate links if present.
>
> That is a serious problem, imho.
>
> [...]
>
> Would it be possible to capture the immediate "parent" in the linkvar
> structure at the point where upvar/variable/global or an -upvar is
> handled?

This is not possible without changing how link variables are created.
Although I understand this can be troublesome, this is imho outside of
the scope of this TIP.

-- Mathieu

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Tcl-Core mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/tcl-core
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TIP #471: Add [info linkedname] Introspection Command

Harald Oehlmann
In reply to this post by Mathieu Lafon
Dear Matthieu,

thank you for the valueable answers.
No expert here, sorry.

Am 17.05.2017 um 09:37 schrieb Mathieu Lafon:

>
>> e) it is a link on a link ?
>>     set a 1
>>     upvar #0 a b
>>     upvar #0 b c
>>     info linkname c
>>     -> a or b ?
>
> This will return "a" *but* not because intermediate links are
> followed, but because c will internally be directly linked to a. [info
> linkedname] will not follow intermediate links if present.

This is interesting. I suppose, there is specification work here.
The argument "implementation is like that" will probably not be a good
argument.

I personally don't see the use-case of the command expect to find out if
a variable is a link in general.
The returned name has no practical use, as the upvar level is not
returned. It is not known, if the target variable is in the stack of a
caller or a global variable. And other introspection commands may not be
executed, as this requires the level information.

I suppose, the glob-pattern version returns a list, which will be empty
if there is no such linked variable.

Another question came to my mind. Is the returned variable name with its
namespace? This would, in a sense, allow to differenciate global and
stack-frame variables...

namespace eval ns { variable a 1}
upvar #0 ::ns::a b
info linkname b

Those questions are not important. I am not in the discussion. You may
see it as poety slam and ignore it.

Thank you,
Harald



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Tcl-Core mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/tcl-core
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TIP #471: Add [info linkedname] Introspection Command

Mathieu Lafon
On Fri, May 19, 2017 at 8:45 AM, Harald Oehlmann
<[hidden email]> wrote:
>> This will return "a" *but* not because intermediate links are
>> followed, but because c will internally be directly linked to a. [info
>> linkedname] will not follow intermediate links if present.
>
> This is interesting. I suppose, there is specification work here.
> The argument "implementation is like that" will probably not be a good
> argument.

This TIP should evolve based on user feedback and enhancement ideas so
this is probably something that can also change, although this will
require more intrusive modifications of how varlink are currently
handled (which may not be desired for only supporting that
introspection command).

> I suppose, the glob-pattern version returns a list, which will be empty
> if there is no such linked variable.

Yes.

> Another question came to my mind. Is the returned variable name with its
> namespace? This would, in a sense, allow to differenciate global and
> stack-frame variables...
>
> namespace eval ns { variable a 1}
> upvar #0 ::ns::a b
> info linkname b

In the current implementation, the namespace is not returned. I will
check if the namespace can be found and how it can be returned.

Regards.

-- Mathieu

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Tcl-Core mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/tcl-core
Loading...