tags:

views:

59

answers:

2

In this code snippet fields-types in the end is modified by to-camel-case function whereas it was passed as a local variable to the parent function:

fields-types: ["First Name" "string" "Last Name" "string" "Age" "int"]

to-camel-case: funct[name][
    name/1: lowercase name/1
    replace/all name " " ""
]

fill-template-body: func[field-labels-types [block!] /local vars fields-names-types][

  vars: [member-name member-type]
  field-names-types: copy []
  foreach [field-label field-type] field-labels-types [
      append field-names-types to-camel-case field-label
      append field-names-types field-type
  ]

]

fill-template-body fields-types

This is a side effect that I wouldn't expect. Couldn't something done in a next rebol version ?

Execution gives:

>> fill-template-body fields-types
== ["firstName" "string" "lastName" "string" "age" "int"]
>> fields-types
== ["firstName" "string" "lastName" "string" "age" "int"]
>>

whereas I would want that fields-types stays invariant:

fields-types: ["First Name" "string" "Last Name" "string" "Age" "int"]

Of course I can try to circumvent this by modifying to-camel-case to use a copy of name but this is not clean and bullet-proof for a language feature.

Maybe something like var and val keywords like in scala could exist.

+1  A: 

Variable is an ugly word in REBOL, everything - even words - are values. This isn't some semantic newspeak, it's helpful in understanding the way in which REBOL flows.

I think of values as being contained within one giant array in memory, where REBOL (the language) uses words and their contexts to reference and interact with the values. Most REBOL functions operate without duplicating these values:

head lowercase next uppercase str: "abcd"
remove back tail str

This is one of REBOL's most efficient features - you don't need copies for intermediary processes, to require such is wasteful. Think of that array growing where every time you use replace, uppercase or to-camel-case a value is duplicated. Whole processes can be built on the premise of modification rather than duplication - indeed a context can be built without necessarily having to return a value:

remove-spaces: use [space mark][
    space: charset " ^-"
    [any [mark: space (remove mark) | skip]]
]

parse/all str: "Should Be No Spaces" remove-spaces

The trick then becomes knowing where to copy values, and I think happens to intersect with REBOL's gift for concise expression:

parse/all link: copy title: "An Article on REBOL" remove-spaces
print ["(" link ")" title]

to-camel-case copy field-label

And of course, modification has its limitations. Sometimes it is cleaner building from scratch.

rgchris
Thanks will reread it several times. Still a mechanism would be usefull to give the choice, maybe something like val keyword http://reboltutorial.com/blog/scala-val-keyword/ ? will have to think about it.
Rebol Tutorial
Seems to me you want to 'protect a value, not a word(?) This doesn't happen in REBOL: foo: bar: "foobar" protect 'foo uppercase bar probe fooProtecting a word only prevents you from assigning it to another value. The value itself can still be manipulated via another reference, unless you use a copy. bar: copy foo: "foobar" protect 'foo uppercase bar probe foo
rgchris
Crap, newlines not preserved in comments...
rgchris
I agree about comments and newline :)
Rebol Tutorial
What I want is a local classical behavior when I need it so I'd like some kind of flag or keyword to do so not everytime because I like the fact that rebol doesn't force locality but sometimes when I decide to. I want the choice to decide myself not that the language to impose to me.
Rebol Tutorial
+1  A: 

Your camel case function operates on the original value so if you want to preserve the original value, you need to copy it, and return the altered value. Since your function acts on a template it needs to copy the template right??

So, something like this should work:

fill-template-body: func[ labels [block!] /local field-labels-types vars fields-names-types][
  field-labels-types: copy labels
..
Graham Chiu
yes that's what I've done but I just say that one should have an automatic mechanism like val keyword in scala maybe so as not to have to think about it each time.
Rebol Tutorial