1. Att: Rob - namespace problem
- Posted by Hayden McKay <hmck1 at dodo.com.au> Sep 22, 2005
- 473 views
There seems to be some "namespace" abnormality under some rare circumstances. 1. I'll demonstrate what I was tryeng to do. 2. I'll demonstrate a hack I found to fix it. I found this problem while codeing a new bst class for an application. An extract from the library is something like this...
-- bst.e constant -- bstNdata = 1, bstLeafL = 2, bstLeafR = 3 type node(sequence s) return length(s) = 3 end type sequence bstTree bstTree = {} global function bstLookup(atom n) -- lookup at bstTree global function bstInsert(atom n) -- insert at bstTree -- etc...
Ok. Now I wanted to use multiple Binary Serch Tree's and the easiest way to do this was to exploit Euphoria's namespace qualifier to create two classes.
-- Too make two instaces of "bstTree" (in bst.e) I did the syntax... include bst.e as BinaryTableOne include bst.e as BinaryTableTwo BinaryTableOne:bstInsert(1) -- Insert to 1st bstTree Inctance BinaryTableTwo:bstInsert(2) -- Insert to 2nd bstTree Inctance
This did not work! Only one instace of "bstTree" existed? However a valid work around was to have two copies of the file with two different names ie
include bstOne.e as BinaryTableOne include bstTwo.e as BinaryTableTwo BinaryTableOne:bstInsert(1) -- Insert at 1st bstTree Inctance BinaryTableTwo:bstInsert(2) -- Insert at 2nd bstTree Inctance
And presto... it worked! The drawback is that I need to have multiple copies of the same bst.e file accompany the source. Maybee this issue could be fixed in a future release. Note: The other alterative was to have a function return a handle. And have "bstTree" store multiple trees.
2. Re: Att: Rob - namespace problem
- Posted by D. Newhall <derek_newhall at yahoo.com> Sep 22, 2005
- 446 views
Hayden McKay wrote: > > There seems to be some "namespace" abnormality under some rare circumstances. > > 1. I'll demonstrate what I was tryeng to do. > 2. I'll demonstrate a hack I found to fix it. > > I found this problem while codeing a new bst class for an application. > An extract from the library is something like this... > (snip) Hehe... I had the exact same thing happen to me. Then I found out the sad truth... that's actually a "feature" of the language. Include files can only be included once no matter what namespace it's declared as, directory it's in, etc.. That also means that include files in different directories with the same name are treated as the same file and included only once. My personal opinion has been that this is the greatest limitation of Euphoria (partially because I came from the Modula-2 world). This could improve the use of "object oriented" coding techniques and would actually DECREASE the amount of bugs in programs (we'd at least have less people post the same "bug over and over :D). The Euphoria Standard Library project : http://esl.sourceforge.net/ The Euphoria Standard Library mailing list : https://lists.sourceforge.net/lists/listinfo/esl-discussion
3. Re: Att: Rob - namespace problem
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Sep 22, 2005
- 467 views
On Wed, 21 Sep 2005 20:54:46 -0700, Hayden McKay <guest at RapidEuphoria.com> wrote: >Ok. Now I wanted to use multiple Binary Serch Tree's and the easiest way to >do this was to exploit Euphoria's namespace qualifier to create two classes. Nope ). >}}} <eucode> >-- Too make two instaces of "bstTree" (in bst.e) I did the syntax... > >include bst.e as BinaryTableOne >include bst.e as BinaryTableTwo > >BinaryTableOne:bstInsert(1) -- Insert to 1st bstTree Inctance >BinaryTableTwo:bstInsert(2) -- Insert to 2nd bstTree Inctance ></eucode> {{{ <snip> >Maybee this issue could be fixed in a future release. >From past discussions, highly unlikely. > >Note: The other alterative was to have a function return a handle. And have > "bstTree" store multiple trees. Yes, the syntax, for comparison with the above, is:
include bst.e constant BinaryTableOne=newTree() constant BinaryTableTwo=newTree() bstInsert(BinaryTableOne,1) -- Insert to 1st bstTree Inctance bstInsert(BinaryTableTwo,2) -- Insert to 2nd bstTree Inctance
D. Newhall wrote: >That also means that include files in different directories with the >same name are treated as the same file and included only once. Yes, I also consider this a bug. I should really pull my finger out and post a solution to this for eu.ex. >This could improve the use of "object oriented" coding techniques and >would actually DECREASE the amount of bugs in programs However I don't think this. The same file in the same directory should only ever be included once. The technique above which allows newTree's to be created dynamically at the cost of passing this id to every function rather than using a namespace qualifier is not particularly expensive either in terms of typing (about the same) or performance (in theory, *at most* 20%, rapidly tending to 0% for ops on trees of any significant size). There would probably be cases where all the duplicated code causes cache misses so it ends up being slower. Another point is that if you wanted to reference a tree in more than one program, presumably you would in the 'class' system code 'include bst.e as BinaryTableOne' in each source (so the interpreter would have to maintain a list of previously used namespace qualifiers against each 'instance' of each include file..); any spelling mistake or typo in 'BinaryTableOne' would create a new tree, with no possibility of error or warning, whereas in the 'newTree' approach you would make the constant BinaryTableOne global, much less error-prone in the long run. Lastly, changing the interpreter to create a second instance because the namespace is different would break many, many things. Regards, Pete
4. Re: Att: Rob - namespace problem
- Posted by Al Getz <Xaxo at aol.com> Sep 22, 2005
- 442 views
Hi there, As far as changing the source to make a regular 'include' be included several times using a new instance... I'd have to agree with Pete, if it's done that way, but if another keyword or something is used such as: include_repeatable MyFile.e as MF_1 include_repeatable MyFile.e as MF_2 i have to say this would be an extreme boost of functionality for Euphoria. This is because basically the .e or .ew file functions as a group of functions with a data class bound with them. Being able to invoke another instance of this file would be almost as important as invoking another variable with statements like "atom", "sequence", etc. Imagine if you could only use a variable name ONE TIME in your program, like "atom x". Imagine how hard it would be to 'not' use it again by accident. You'd have to keep changing the name every time you needed a variable like that: atom x atom x1, atom x2, atom xx, etc. This is similar to the restriction now imposed by the include statement, where you cant bring about another instance without changing the file name. Think about this...everything is exactly the same except the file name itself. Also, this would bring into being another 'scope' for variables like shared variables. Shared variables could be a scope for local or global variables that you want to be the *same* in ALL file instances. For example: --MyFile.e atom x,y shared atom z x=0 y=0 z=0 global procedure UpdateXYZ(atom xx,atom yy,atom zz) x=xx y=yy z=zz end procedure global function GetXYZ() return {x,y,z} end function In instance 1 and 2 of MyFile.e x and y are local variables, but z is the same for both files so that if either file changes z it's reflected in BOTH files, not just one. It's not global, however, because only the instances can see them. include_rep MyFile.e as MF1 include_rep MyFile.e as MF2 ?MF1:GetXYZ() --prints {0,0,0} ?MF2:GetXYZ() --prints {0,0,0} MF1:UpdateXYZ(1,2,3) ?MF1:GetXYZ() --prints {1,2,3} ?MF2:GetXYZ() --prints {0,0,3} You can see how interesting this addition would be and that it could never 'hurt' the language in any way. Also, if the 'shared' concept was extended to routines also, routines could be shared among several 'class instances' meaning the overall program length wouldnt increase much if you only had say one sequence that had to be unique among all the instances. BTW, i chose the keywords somewhat randomly so there might be better names. There might also be additional ideas that come about once the multiple instance concept is accepted which can only increase the functionality of the language. The trace mechanism would have to be updated to include the namespace prefix with the file name so that the user would know which instance they were stepping through... MF1:MyFile.e MF2:MyFile.e etc. Take care, Al And, good luck with your Euphoria programming! My bumper sticker: "I brake for LED's"