5

而不是通过编写来创建对象:

obj: object [
    name: "Fork"
    id: 1020
]

我想写一些类似...

obj: something-or-another [name id] ["Fork" 1020]

...并得到相同的结果。理想的解决方案还允许:

obj: something-or-another [name: id:] ["Fork" 1020]

很容易写 a something-or-another,但这是否适合已经“装在盒子里”的东西?

4

3 回答 3

3

我不相信有一种成熟的方法可以做到这一点。不过不难:

func [words values][
    set words: context append map-each word words [to set-word! word] none values
    words
]

我想我可以稍微分解一下:

func [
    "Creates an Object From a Block of Words, Assigns Values"
    words [block!] "Words used to create object"
    values "Value(s) to assign"
][
    words: map-each word words [to set-word! word] ; The requisite set-words
    append words none ; Initial value for all words
    words: context words ; Create our object
    set words values ; Assigns our value(s) to the object
    words ; returns the object
]

您可以采用不同的方法来交错块,例如:

func [words [block!] values [block!]][
    collect [
        repeat index max length? words length? values [
            keep words/:index
            keep values/:index
        ]
    ]
]
于 2013-08-21T02:59:17.667 回答
2

这是至少需要 Rebol 3 的东西:

func [
    "Create an object based on some words and values."
    words [any-word! block!] "Word or block of words"
    values [any-type!] "Value or block of values"
    /local object
][
    object: make object! either block? words [length? words] [1]
    set bind/copy/new :words object :values
    object
]

如果您还想允许设置未设置的值,请​​尝试以下操作:

func [
    "Create an object based on some words and values."
    words [any-word! block!] "Word or block of words"
    values [any-type!] "Value or block of values"
    /any "Allows setting words to any value, including unset"
    /local object
][
    object: make object! either block? words [length? words] [1]
    apply :set [bind/copy/new :words object :values any]
    object
]

这两个都使用 来创建对象self,所以如果你想创建一个没有对象的对象,self你必须做一些更花哨的技巧。有关详细信息,selfless请参阅提案

于 2013-08-23T15:12:09.853 回答
1

就在几天前,我写了一个类似的函数(Rebol2):

build-object: func [
    "Builds an object from a block"
    names [block!] "Field names"
    /values val [block!] "Initial values"
    /local o name value
] [
    o: copy []
    names: compose names
    o: either values [
        parse names [
            some [
                set name [word! | set-word!]
                (append o to-set-word name)
                | skip
            ]
        ]
        set/pad reflect o: context append o none 'words val
        o
    ] [
        if any [
            parse names [
                some [
                    set name [word! | set-word!]
                    (append o reduce [to-set-word name none])
                ]
            ]
            parse names [
                (clear o) some [
                    set name [word! | set-word!] set value any-type!
                    (append o reduce [to-set-word name :value])
                ]
            ]
        ] [context o]
    ]
    o
]

要构建您的对象,您可以编写以下任何内容:(创建一个函数为f: does ["x"]

  • build-object [name "Fork" id 1020]
  • build-object [name: "Fork" id: 1020]
  • build-object/values [name id] ["Fork" 1020]
  • build-object/values [name: id:] ["Fork" 1020]
  • build-object [name f]
  • build-object [name (f)] ;block is composed
  • build-object [name 5 id f]
  • build-object [name 5 id 'f]

none如果您将值排除在外,您还可以制作字段设置为的对象,例如使用

build-object [name id]
于 2013-08-21T07:35:47.787 回答