Rebol:为什么 get-access-modifier 被调用两次而它应该只被调用一次

发布于 2024-09-07 21:09:09 字数 1239 浏览 5 评论 0原文

在 rebol 控制台中输入时,

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

我得到 get-access-modifier 调用两次:

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):

而它在源代码中只提到一次:

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

我真的不明白为什么,你能吗?

原始代码(互联网档案馆)

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:

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

I really can't see why, can you ?

Original code here (Internet Archive)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2024-09-14 21:09:09

是的。只有一次对它的调用,但它位于 foreach 内部。您的默认值是两个字段,因此您会被询问两次。输入更多,您会被问到更多。

虽然您可以(并且可能应该)将其保存在变量中,但 Rebol 还有其他方法。例如,您可以组合代码块:

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
]

组合运行一次,深入查找代码块中的括号,并计算代码。 (类似于解析看到括号时的方式)。剩下的就不用管了。因此,完成替换的块将被传递到 FOREACH 中以运行循环。

只是您如何可能有一个看似在循环内但仅执行一次的调用的细微差别。我不建议将它用于类似的事情。

建议的是通过学习更多的 Rebol 原语(例如 REJOIN...它从一个块中构建一系列)来研究如何减少代码中的冗余。系列类型将匹配它看到的第一个类型(如果第一个元素不是系列,则为字符串):

modifier: get-access-modifier ;-- called only once, stored in variable

foreach field-layout fields-layout [
    append fields-template-output rejoin [
        "        "
        (to-string modifier)
        field-layout
        ";"
        newline
    ]
]

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 evaluates the code. (Kind of how parse does when it sees parentheses). The rest is left alone. So the block with the substitutions done 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. I wouldn't suggest using it for something like this.

What I would suggest is studying making things less redundant in your code, by learning some more Rebol primitives like REJOIN...which builds a series out of a block. The series type will match whatever the first type it sees is (or a string if the first element is not a series):

modifier: get-access-modifier ;-- called only once, stored in variable

foreach field-layout fields-layout [
    append fields-template-output rejoin [
        "        "
        (to-string modifier)
        field-layout
        ";"
        newline
    ]
]
牵你手 2024-09-14 21:09:09

为了解决这个问题,我使用 static var 来检测它只执行一次(感谢 Sunanda 提示 rebol 函数内是否可以有静态变量?)。

ask-params: function [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
]

To solve the problem I have used static var to detect that it is executed only once (thanks to Sunanda tips is it possible to have static variable inside a rebol function? ).

ask-params: function [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
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文