tags:

views:

28

answers:

2

When typing in rebol console

do read http://askcodegeneration.com/csharp/simple-class/

I get get-access-modifier called twice:

Access modifier:
1. private: member can be accessed only by code in the same class
2. protected: member can be accessed only by code in the same class or in a derived  class
3. internal: member can be accessed only by code in the same assembly
4. public: member can be accessed by code in the same assembly or another assembly that references it choice (by default 1): 
Access modifier:
1. private: member can be accessed only by code in the same class
2. protected: member can be accessed only by code in the same class or in a derived  class
3. internal: member can be accessed only by code in the same assembly
4. public: member can be accessed by code in the same assembly or another assembly that references it choice (by default 1):

whereas it is only mentioned once in the source code (source code is too lengthy to paste it here so see it here http://askcodegeneration.com/csharp/simple-class/ ):

append fields-template-output  form reduce [(to-word get-access-modifier) field-layout]

I really can't see why, can you ?

+1  A: 

Yes. There is only one call to it, but it's inside of a foreach. Your default is two fields, so you get asked twice. Enter more, you'll get asked more.

While you could (and probably should) do the obvious thing of saving it in a variable, Rebol has other ways. For instance you could compose the block of code:

foreach field-layout fields-layout COMPOSE/DEEP [
    append fields-template-output  "        "
    append fields-template-output  form reduce [
        to-word (get-access-modifier) field-layout
    ]
    append fields-template-output  ";"
    append fields-template-output  newline
]

The composition runs once, looks deep for the parentheses in the block, and executes the code. (Kind of how parse does when it sees parentheses). The resulting block is what's passed into foreach to run the loop.

Just a nuance of how you could have a call that appears to be inside a loop and yet is executed only once. Don't do it for something like this.

Hostile Fork
...and you know what I'm going to say about paring down code to generate a minimal test case! You will usually solve simple mistakes like this on your own if you do!
Hostile Fork
Oops stupid of me, I'm not accustomed to code professionally any more and sometimes basics just escape me :)
Rebol Tutorial
A: 

To solve the problem I have used static var to detect that it is executed only once (thanks to Sunanda tips http://stackoverflow.com/questions/2860925/is-it-possible-to-have-static-variable-inside-a-rebol-function ).

ask-params: funct[config-file [file!] default-class-name default-fields][
  ;config-file: %askcodegeneration.com/csharp/simple-class/simple-class.params.txt


  either value? 'class-name [
     ans: ask rejoin ["class name" " (by default " class-name ")" ": "]
     if ans <> "" [class-name: ans]
  ][
     class-name: default-class-name
     ans: ask rejoin [{class name (default "} class-name {"): }]
     if ans <> "" [class-name: ans]
  ]

  either exists? it: config-file [
      params-block: copy load it
  ][
      params-block: []
  ]

  either res: find params-block class-name [
      fields: pick res 2
      print [ class-name "found in" it]
  ][

      fields: default-fields
      ans: ask rejoin [{fields (by default } {"} fields {"} {): }]
      if ans <> "" [fields: ans]

      new-param: reduce [
      mold class-name
      mold fields
      ]

      either not exists? config-file [
          create-directory "askcodegeneration.com/csharp/simple-class/"
          write/append config-file reform new-param
      ][
          write/append config-file reform [newline new-param]
      ]

  ]
  append ret-value: [] class-name
  append ret-value fields

  ret-value
]
Rebol Tutorial