World: r3wp
[Core] Discuss core issues
older newer | first last |
Henrik 8-Jan-2010 [15382] | setting no global words: use a context |
Janko 8-Jan-2010 [15383x3] | I imagine when you now set some word in some function it looks at /local words and it it's there it creates a local word, if not it just sets a word (globaly).. let's say that there is funcstrinct that in second case just throws an error (probably something like that could be made in rebol itself) |
Henrik: but I don't want to make each function a context (probably:) ) .. I have to admit I don't know much about contexts .. only that it's like object | |
I am changing to using a context now for all words that are defined outside functions on rps pages to make them local to that pageload | |
Dockimbel 8-Jan-2010 [15386x2] | Just remembered about querying system/words, that would give you a mean to detect new global words. |
>> query/clear system/words == [end! unset! error! datatype! context! native! action! routine! op! function! object! struct ! library! port! any-type! any-word!... >> context [set 'a 5] >> query system/words == [a] | |
Janko 8-Jan-2010 [15388] | cool! I was looking at system/words but had no idea how to see my words in there ! didn't even know for query word so far .. cool |
Dockimbel 8-Jan-2010 [15389] | Detecting at runtime is the only way. So, you could write your funcstrict function using this 'query trick, but that would cost you 2 'query calls each time the function is called... |
Henrik 8-Jan-2010 [15390] | janjo, you'll find that using contexts will help you solve these problems. it's the next best thing to modules. |
Janko 8-Jan-2010 [15391] | Doc .. thanks a lot for that query word ... it' awesome to see on a page what all got set in the process.. this will help me the to make code more strong A LOT! Doc.. how does the runtime binding to function local words work? func is probably not a mezzaine or something where we could peek into what it does with /local words ? Henrik: I need to learn more about them and how to use them .. are there any good docs to read about what contexts are used for maybe? |
Henrik 8-Jan-2010 [15392] | Janko, try: http://blog.revolucent.net/2009/07/deep-rebol-bindology.html |
Dockimbel 8-Jan-2010 [15393] | Janko, a function is a context! value like objects. You can use the following mental analogy to see how it is related : foo: func ["for demo" a [integer!] /local b][...] would be *roughly* equivalent to constructing an object like that : foo-def: make object! [ hidden-ctx: make object! [a: none local: none b: none] body: [...] spec: ["for demo" a [integer!] /local b] ] The body is bound to the 'hidden-ctx context at function creation. When calling 'foo, the interpreter will set the 'hidden-ctx object words values according to passed arguments and refinements and then DO 'body. There's no differences on how REBOL treats "arguments" and "local words", it's part of the illusion. The /local refinement is used by *convention* only, to set "local words", you could just decide to use any other refinement for the same job. Here's an example : >> a: func [/local b][print b] >> a/local 5 5 Additionnaly, when you apply the ordinal natives on a function! value, you get : >> first :foo == [a /local b] ;=> the hidden context words >> second :foo == [...] ;=> the function body block >> third :foo == ["for demo" a [integer!] /local b] ;=> the original spec block |
Henrik 8-Jan-2010 [15394] | trying: source context might be a revelation too. |
Janko 8-Jan-2010 [15395x4] | huh.. plenty of info, might need some time to process this to get all that you two meant . but I did already know first :foo second: foo stuff (I was playing with rebol to js compiler) and I did know that context is object |
hm.. if rebol binds hidden ctx to function body then it really can't do anything to trigger warning on global words. (if I understand things aroung bind correctly) | |
ok .. query/clear will find me seek out leaking globals so this problem has got a solution in a way | |
if function uses objects and bind internally or something like that, then objects in rebol and bind should be cheap right? and then an object is cheaper than function ? | |
Dockimbel 8-Jan-2010 [15399x3] | if rebol binds hidden ctx to function body => It's the other way around, "binds body to ctx". BIND will only link the words that match those defined in the target context, to that same context, nothing more. |
Object! and function! have different creation and usage semantics, AFAIU, they share a common internal parent datatype, context!. So, context! (which is not directly accessible) should be "cheaper". Objects and functions have different purposes, so this might be like comparing apples and oranges...in a closed blackbox. | |
BIND on a block! does a full recursive traversal of the block! and for each word! found, does a fast lookup in the target context (probably hashed). So the cost is directly proportional to the size and depth of the argument block!. | |
Terry 8-Jan-2010 [15402x4] | Is it just me, or does anyone else find JOIN and REJOIN cumbersome? |
ie: Comparing to PHP $var = "Hello World"; echo "This is $var in the sentence"; | |
Rebol var: "Hello World" print rejoin ["This is " var "in the sentence"] | |
That's not too bad, but when you start including single quotes, double quotes, multiple variable etc, it becomes more difficult | |
Henrik 8-Jan-2010 [15406] | well, how else would you do it? I guess you need kind of an escape sequence to evaluate a word or something. |
Steeve 8-Jan-2010 [15407x3] | there is plenty of functions to do such, the simple one is the one you can do |
Terry, show use case you think is cumbersome and we'll show you a simpler way (i hope) | |
did you tried REWORD ? | |
Claude 8-Jan-2010 [15410] | terry: var={coucou} print rejoin [{hello + } var { à vous}] |
Terry 8-Jan-2010 [15411x4] | $varA =<<<VA \'hello\'s {Worlds \'; VA; $varB =<<<VB 'and {so "on'; VB; $n=<<<HD <button onclick="alert('$varA ');">CLICK ME</button> $varB HD; echo $n; |
<<< is a here-doc in php | |
(output is JS) | |
so.. the result is a button that alerts.. \'hello\'s {Worlds \'; ...and prints... 'and {so "on'; after the button | |
Steeve 8-Jan-2010 [15415] | I see nothing you can't do with rebol, so what ? |
Terry 8-Jan-2010 [15416x2] | um.. i don't think that was the point |
Show me the 'simpler way' | |
Steeve 8-Jan-2010 [15418x2] | I just can't figure the exact string output you want. |
don't mess it with escaping characters or comments. | |
Terry 8-Jan-2010 [15420] | no comments.. that's functioning php.. and if you don't escape it, it doesn't function as JS |
Gregg 8-Jan-2010 [15421] | There has been talk in the past of including a substituion function, REWORD being the R3 func for it. In R2 we have build-markup, which shouldn't be hard to hack, but I don't know of a version that anyone has done for a given substitution syntax. |
Steeve 8-Jan-2010 [15422] | Terry, i don't think your output must be { \'hello\'s {Worlds \'; ...and prints... 'and {so "on'; } And yet, that's what you show us actually |
Gregg 8-Jan-2010 [15423x2] | build: func [ {Return text replacing $tags with their evaluated results.} content [string! file! url!] /quiet "Do not show errors in the output." /local out eval value ][ content: either string? content [copy content] [read content] out: make string! 126 eval: func [val /local tmp] [ either error? set/any 'tmp try [do val] [ if not quiet [ tmp: disarm :tmp append out reform ["***ERROR" tmp/id "in:" val] ] ] [ if not unset? get/any 'tmp [append out :tmp] ] ] parse/all content [ any [ end break | " $" [copy value to " " | copy value to end] (eval value) | copy value [to " $" | to end] (append out value) ] ] out ] |
Now, that's norribly naive, and doesn't work because of that. e.g. it needs a space before the $ marker, so a var at the beginning of the text gets missed. | |
Steeve 8-Jan-2010 [15425] | yes seems a little messy Greg ;-) |
Gregg 8-Jan-2010 [15426] | Well, what do you in five minutes? ;-) |
Terry 8-Jan-2010 [15427] | Spend 6 :) |
Gregg 8-Jan-2010 [15428x2] | Needs a different name too, as Ladislav has a nice BUILD func that works on blocks. |
I need a spec first. ;-) | |
Terry 8-Jan-2010 [15430x2] | Although, i would be impressed if it didn't choke while trying to escape stuff. |
If it was a smple matter of replacing variables with values.. i have some °7° code that does that. The problem is well formed javascript to send back to the DOM. | |
older newer | first last |