explore current scope, or other hack

spir denis.spir at gmail.com
Tue Nov 16 03:27:53 PST 2010


On Mon, 15 Nov 2010 21:35:19 +0100
"Simen kjaeraas" <simen.kjaras at gmail.com> wrote:

> spir <denis.spir at gmail.com> wrote:
> 
> > On Mon, 15 Nov 2010 12:44:24 -0500
> > bearophile <bearophileHUGS at 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



More information about the Digitalmars-d-learn mailing list