Re: something similar to "Attribute access syntax" sighted
David L. Nicol wrote: > in the tcsh 6.10.00 WishList file: > > [...] > > > echo $a:[#] > > 16 > > > echo $a:[6-7] > > is > > > echo $a:[-2] > > ng > > [...] Iconesque. In Icon, if symbol x holds a string, x[2:3] means substr($x,2,1); but if x holds a list (array), it means @x[2,3]. Similarly, *x is length($x) or scalar(@x) or scalar(keys %x), depending. And, one I've always liked: ?x for a string is a random character, and for a list or table, a random element. -- Sean M. Burke[EMAIL PROTECTED]http://www.spinn.net/~sburke/
something similar to "Attribute access syntax" sighted
in the tcsh 6.10.00 WishList file: > * New idea. > Lots of people seem to like the idea to be able to do sed type > operations on shell variables. Maybe we can extend the syntax > of the variable editing to understand string operations. > So I would like to be able to use: > > set a="this is a STRING" > > echo $a:[3-] > is is a STRING > > echo $a:[#] > 16 > > echo $a:[6-7] > is > > echo $a:[-2] > ng > > echo $a:[-20] > Subscript out of bounds. > > echo $a:[2-20] > Subscript out of bounds. > > echo $a:[1-1]:u$a:[2-]. > This is a string. -- David Nicol 816.235.1187 [EMAIL PROTECTED] "I must report that I am in the fortunate position of having logged and categorized my nightmares over the past 37 years." -- Bob Dehnhardt
Re: Attribute access
From: "Nathan Wiger" <[EMAIL PROTECTED]> Sent: Friday, September 29, 2000 12:51 AM > James Mastros wrote: > > As far as setting|getting, I'd like to make a simple proposal. Consider it > > an idea for whoever writes the RFC (I'm looking at you, Nate) > Oh, jeez, as if I didn't have enough already! ;-) Well, you did rather say you did. > > The idea is this: every variable should have a psudohash of attributes. > Idea is good, implementation I think should be different. Especially > since Schwern has professed "pseudohashes must die!". Oh, sorry. By psudohash I meant a hash that doesn't really exist as a hash of it's own, not a hash that's really an array. > > my $subsline = $&sub:{line}; > However, overall I think this should be hidden. The internal magic of > attributes will likely not map 1:1 to hash keys, so requiring what is > essentially an accessor function for these is probably a good thing. The internal magic I don't know, but attributes, to me, seem close enough to hashes that throwing away all the power of hashes seems silly and unperlish. (After all, an object is a ref, and a stack is an array.) Anyway, how do you do a foreach (keys attrib $scalar)? Best of both worlds: Have attrib return a magicly lvalueable hash (list) in one-arg form. > That way, attributes can be applied to objects, packages, subs, and > other things that don't have variable representations: Hmm... I thought that a sub was a variable with a & funny-char (with some oddities, such as &sub calling sub and giving that value, rather then &sub itself), and "An object is simply a reference that happens to know which class it belongs to". As to packages, I'd set the attributes on their namespace hash -- %%package::name:::{'fluffy'}++; (Three colons on the end.) OK, that's pretty ugly, I'll admit, which is why the alternate syntax is there. > The declaration of attrs is proposed vaguely in RFC 337. I'll add access > before the freeze. Gah, I forgot that one. Hmm, in 279, you propose a syntax like this: LVALUE : ATTRIBUTE; However, this syntax would make this: $a[0] :32bit = get_val; # 32-bit reasonably mean Get the current value of $a[0], set the 32bit attribute on it, and then replace that value with the return of the get_val function. This would end up making $a[0] = get_val with no attributes, since the return of the get_val function (assumedly) has no attributes. -=- James Mastros
Re: Attribute access
James Mastros wrote: > > As far as setting|getting, I'd like to make a simple proposal. Consider it > an idea for whoever writes the RFC (I'm looking at you, Nate) Oh, jeez, as if I didn't have enough already! ;-) > The idea is this: every variable should have a psudohash of attributes. Idea is good, implementation I think should be different. Especially since Schwern has professed "pseudohashes must die!". > To access the attribhash, you'd use somthing like this: > my($scalar,@array,%hash,^placeholder,&sub); > my %arraysattribs = %@array:; > my $subsline = $&sub:{line}; > > print "\%hash has the following attributes:\n"; > foreach (keys %%hash:) { > print "\$\%hash:{$_} = ", $%hash:{$_}, "\n"; > } > > $subsattribsref = \%&sub:; Here's what I'm thinking: a new builtin, attr(). This might also be implementable as UNIVERSAL::attr. Syntax would be such: @attrs = $object->attr; @attrs = attr %hash; $is_fluffy = attr %hash, 'fluffy'; # query a specific attribute Another keyword could be 'is', which is cuter: $can_puff = is %hash, 'fluffy'; but it's way too close to 'isa' for my tastes. If attributes were the proper tense then 'has' would work: $nice_coat = has $spot, 'fluffiness'; which I kinda like. The attr keyword could be overloaded to also do sub/struct-like definitions, as specified in RFC 337. However, overall I think this should be hidden. The internal magic of attributes will likely not map 1:1 to hash keys, so requiring what is essentially an accessor function for these is probably a good thing. That way, attributes can be applied to objects, packages, subs, and other things that don't have variable representations: package BigInt : autotie; package main; if ( has BigInt 'autotie' ) { # class is automatically tied... } The declaration of attrs is proposed vaguely in RFC 337. I'll add access before the freeze. -Nate
Attribute access
There are many (good) RFCs that specify new attributes. To the best of my knowledge, there is no good RFC discussing how to go about making attributes, setting them, and getting them. As far as setting|getting, I'd like to make a simple proposal. Consider it an idea for whoever writes the RFC (I'm looking at you, Nate) -- I don't have the time to write or maintain an RFC. The idea is this: every variable should have a psudohash of attributes. To access the attribhash, you'd use somthing like this: my($scalar,@array,%hash,^placeholder,&sub); my %arraysattribs = %@array:; my $subsline = $&sub:{line}; print "\%hash has the following attributes:\n"; foreach (keys %%hash:) { print "\$\%hash:{$_} = ", $%hash:{$_}, "\n"; } $subsattribsref = \%&sub:; This is to say, the attribhash for a varable VAR (where VAR includes the funny-character is the hash VAR:. (You can use any expression with a valid result for VAR.) In 5.6, C has the same effect as C, and C is a syntax error. You need to keep the funnychar of the attributed thingy somewhere in the attribhash syntax, since otherwise $foo's attribues aren't seperable from @foo's. The reason you want an attribute hash is fairly obvious, I think. An operator with rvalue and lvalue semantics is also possible: %foosattribs = attribs @foo; %foosattribs{private}++; However, I can't see how you could set an attribue on a value without making a temporary. Variable names containing a : that isn't part of a :: would be reserved. (That, or the bare colon is an operator returning a hash (not a list).) You can also set an attribute like this: my $foo :private; This is equivlent to: my $foo; $$foo:{private} = 1; my $foo :private(1); is equivlent to my $foo; $$foo:{private} = [1]; my $foo; means that exists($$foo:{private}) is false. You can put an attribute in anything that can be used as a variable: Whole arrays. Whole hashes. Scalars. Array elements. Filehandles [Globs]. Subs. Hash values. Hash key-value pairs? This probably makes sense iff hash keys become full scalars and not strings that act like scalars sometimes. It would be useful, though. Note the last one especialy. $$hash{elem}:{raccess}++; $hash{elem} = 42; exists $$hash{elem}:{raccess}; #is false. This is because %$hash{elem}: sets the attribute on the value of $hash{elem}, not on the spot in the hash. I don't know what a good syntax for the hash position would be. And I've got other work to do now. -=- James Mastros Creating attributes: -- -BEGIN GEEK CODE BLOCK- Version: 3.1 GU>CS d->-- s-:- a20 C++ UL+++@ P+++>+ L++@ E-() N o? K? w@ M-- !V PS++ PE Y+ PGP(-) t++@ 5+ X+++ R+ tv+ b+++ DI+ D+ G e>++ h! r- y? --END GEEK CODE BLOCK--