如何将IN与块而不是对象一起使用? [英] How to use IN with a block instead of an object?

查看:77
本文介绍了如何将IN与块而不是对象一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rebol中的IN函数可确定对象中是否包含字段:

The IN function in Rebol finds out if a field is in an object:

USAGE:
    IN object word

DESCRIPTION:
    Returns the word or block in the object's context.
    IN is a native value.

ARGUMENTS:
    object (any-object! block!)
    word -- (modified if series) (any-word! block! paren!)

声称它适用于对象或块.如果我尝试将其与对象配合使用,就可以了:

The claim is it works with objects or blocks. It works okay if I try it with an object:

>> in object [foo: 10 bar: 20] 'foo
== foo

但是,如果我只是尝试使用原始块,它将返回NONE:

But if I just try it with a raw block, it gives back NONE:

>> in [foo: 10 bar: 20] 'foo
== none

猜猜我会理解它是否不支持块(Rebol2不支持).但是在什么情况下,不会不返回任何允许使用块的情况?

Guess I'd understand if it didn't support blocks (Rebol2 didn't). But what's the case in which it wouldn't return NONE that blocks are allowed for?

冒着一口气提出这两个问题的风险,接受BLOCK有什么用! word参数?我认为,如果您要查找一组单词,可能会需要一个代码块,但它似乎只是返回了代码块:

And at the risk of making this two questions in one, what's the deal with accepting BLOCK! for the word parameter? I'd think it would take a block if you had a set of words you were looking for, but it seems to just return the block:

>> in object [foo: 10 bar: 20] [mumble frotz bar]
== [mumble frotz bar]

>> in object [foo: 10 bar: 20] [mumble frotz]
== [mumble frotz]

冒着进一步提出这三个问题的危险,PAREN意味着什么!有话有吗?

And at the further risk of making this three questions, what meaning would taking a PAREN! for word have?

推荐答案

IN函数并不是主要用于在对象中搜索单词,而是用于返回 in 对象,或者用Rebol术语绑定到对象.这是BIND函数的一种变体,在不同于BIND的情况下很有用.

The IN function isn't primarily meant for searching an object for a word, it is for returning a word or block of words that are in the object, or in Rebol terms bound to the object. It's a variant of the BIND function that is useful in different circumstances than BIND.

如果对象中没有该名称的单词,那么它就不返回任何单词,足够明智.这给它带来了附带好处,它可以在条件表达式中用作单词检测功能,但这并不是它的主要用途.

If there is no word of that name in the object, it returns none, sensibly enough. This gives it the side benefit of being able to be used in conditional expressions as a word detection function, but that's not its main use.

IN可以将一个对象块用作其对象参数,然后尝试将该单词依次绑定到每个对象,返回绑定到该单词中具有该单词的块中第一个对象的单词.对于Rebol 3 GUI的旧设计,这是一种相对较快的方法来覆盖单词查找,但是事实证明,这并不是最佳方法,因此GUI现在不使用该设计.但是,IN 阻止词被保留为将来可能在其他地方使用的潜在有用功能.

IN can take a block of objects for its object parameter and then try to bind the word to each object in turn, returning a word bound to the first object in the block that has that word. This was meant as a relatively quick way to do overriding word lookup for an old design of the Rebol 3 GUI, but it turned out to not be the best approach in that case so the GUI doesn't use that design now. However, IN block word was kept as a potentially useful feature for future use elsewhere.

在您的示例中,该块中没有对象,因此IN不能将单词绑定到该对象.这就是IN不返回任何内容的原因.

In your example, there were no objects in that block, so there was nothing that IN could bind the word to. That is why IN returned none.

如果为单词参数传递一个块,它将在返回该块之前将该块绑定到该对象.这是为了允许像do in obj [print a]这样的代码.它不会执行bind/copy,因此您应该注意类似的代码.还是应该复制 object block 中的内容-更改该内容是否为时已晚?传递单词参数的括号应具有与传递块相同的绑定行为.

If you pass IN a block for the word parameter, it binds the block to the object before it returns it. This is to allow code like do in obj [print a]. It doesn't do a bind/copy though, so you should be careful about code like that. Or maybe IN object block should copy - is it too late to change that? Passing a paren for the word argument should have the same binding behavior as passing a block.

最后一次检查时,我们不支持将对象和单词参数都设为块.这是因为至少在很明显的情况下,我们无法为该行为找到一个好的模型.覆盖单词的查找实际上不能与单词块一起使用,因为结果不确定.

When last I checked, we didn't support having both the object an word parameters be blocks. This is because we couldn't figure out a good model for what that behavior should be, at least obviously. The overriding word lookup doesn't really work with a block of words because the results would be inconclusive.

具有将块按顺序反弹到一系列对象的功能很有用,特别是对于重新创建Rebol并非真正嵌套的作用域,但是将该功能设置为IN确实会造成混淆.

It would be useful to have a function that rebound a block to a series of objects in order, especially for recreating Rebol's not-really-nested scopes, but it would be really confusing for that function to be IN.

这篇关于如何将IN与块而不是对象一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆