Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 15, 2010 explore current scope, or other hack | ||||
---|---|---|---|---|
| ||||
Hello, Is there a way to explore the current scope, meaning the set of currently defined symbols? (Equivalent of python's vars(), locals(), globals().) I have 2 use cases for this: 1. name objects automatically I need some objects to know their name (as field on themselves). the only solution I can else imagine is for the user write: x = ...; x.name = "x"; I hope you agree this is more than stupid ;-) Exploring the scope would allow providing a tool func that does this automatically, once all objects are known. Is there an alternative I overlook? 2. some tool like format() Say I wish like to define an alternative approach to variable strings, like s = VarString("Hello, 'userName'!"); This requires getting variable values at runtime, I guess, meaning explore the scope. How do format and writefln work (pointer welcome)? Is there an alternative? Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com |
November 15, 2010 Re: explore current scope, or other hack | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | spir:
> 1. name objects automatically
> I need some objects to know their name (as field on themselves). the only solution I can else imagine is for the user write:
> x = ...;
> x.name = "x";
What if you have two or more references to the same object?
Regarding your generic question, I'd like DMD to have more static introspection, for for now ...
Bye,
bearophile
|
November 15, 2010 Re: explore current scope, or other hack | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Mon, 15 Nov 2010 12:44:24 -0500 bearophile <bearophileHUGS@lycos.com> wrote: > spir: > > > 1. name objects automatically > > I need some objects to know their name (as field on themselves). the only solution I can else imagine is for the user write: > > x = ...; > > x.name = "x"; > > What if you have two or more references to the same object? I'm not sure what you mean. The said objects are patterns (instances of a Pattern class hierachy). And indeed, they are multiply referenced (that's precisely the point of writing a grammar ;-). Their names are used everywhere: * stand-alone pattern output * super-pattern output (subpatterns are replaced by their name, else output explodes) eg operand: symbol|number * inside nodes they generate (eg allow discrimination between choices) * error output ("expected operand at position...") * result output for testing > Regarding your generic question, I'd like DMD to have more static introspection, for for now ... Dynamic introspection would do the job for me, I guess. In python, would be like (untested): def namePatterns (scope = var()): for (name,obj) in scope: if isinstance(obj, Pattern): obj.name = name Don't know how static would help better. Any idea how else to write somethink analog to format()? denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com |
November 15, 2010 Re: explore current scope, or other hack | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | spir <denis.spir@gmail.com> wrote: > On Mon, 15 Nov 2010 12:44:24 -0500 > bearophile <bearophileHUGS@lycos.com> wrote: > >> spir: >> >> > 1. name objects automatically >> > I need some objects to know their name (as field on themselves). the >> only solution I can else imagine is for the user write: >> > x = ...; >> > x.name = "x"; >> >> What if you have two or more references to the same object? > > I'm not sure what you mean. The said objects are patterns (instances of a Pattern class hierachy). And indeed, they are multiply referenced (that's precisely the point of writing a grammar ;-). The two (or more) references could not be referred to by the same name. Sure, they would point to the same object, but how could the object know whether its functions were called from x or from y? While you may use the same name everywhere for the same object, the x in function foo is a different one from that in function bar, not to mention across modules. If I may come with a solution, it would be something like this: mixin template New!( T, string name ) { mixin( "auto " ~ name ~ " = new T(\"" ~ name ~ "\")"); } -- Simen |
November 16, 2010 Re: explore current scope, or other hack | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | On Mon, 15 Nov 2010 21:35:19 +0100 "Simen kjaeraas" <simen.kjaras@gmail.com> wrote: > spir <denis.spir@gmail.com> wrote: > > > On Mon, 15 Nov 2010 12:44:24 -0500 > > bearophile <bearophileHUGS@lycos.com> wrote: > > > >> spir: > >> > >> > 1. name objects automatically > >> > I need some objects to know their name (as field on themselves). the > >> only solution I can else imagine is for the user write: > >> > x = ...; > >> > x.name = "x"; > >> > >> What if you have two or more references to the same object? > > > > I'm not sure what you mean. The said objects are patterns (instances of a Pattern class hierachy). And indeed, they are multiply referenced (that's precisely the point of writing a grammar ;-). > > The two (or more) references could not be referred to by the same name. Sure, they would point to the same object, but how could the object know whether its functions were called from x or from y? While you may use the same name everywhere for the same object, the x in function foo is a different one from that in function bar, not to mention across modules. Oh, yes! I see the point, now, thank you. But in the present case (and a few others where I had to name objects), only the "birth" name is relevant. Here is an example from written using the library I'm writing, I'm sure it's self-explaining. I overloaded opCall in the top Pattern class so that it names the object: ================== test code ==================== auto digits = new String(new Klass("0-9")); digits.name = "digits"; auto DOT = new Literal("."); DOT.name = "DOT"; auto integer = new Compose(digits,DOT,digits); integer.name = "integer"; // output writeln(digits.view()); writeln(integer.view()); writeln-); integer.test("123.45"); writeln-); integer.test("123."); =================== output ====================== digits=[0-9]+ integer=(digits DOT digits) --------------------------------- pattern : integer=(digits DOT digits) text : "123.45" outcome : "(123 . 45)" --------------------------------- Test Error: see outcome below. --------------------------------- pattern : integer=(digits DOT digits) text : "123." outcome : "*failure*" --------------------------------- ********************************* End Of Text Error: end of text reached while matching pattern digits=[0-9]+ at index 4 ********************************* > If I may come with a solution, it would be something like this: > > mixin template New!( T, string name ) { > mixin( "auto " ~ name ~ " = new T(\"" ~ name ~ "\")"); > } Thank you again, I'll explore this path. Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com |
Copyright © 1999-2021 by the D Language Foundation