Rebol 中的共享函数(实现继承)

发布于 2024-08-17 05:32:23 字数 461 浏览 2 评论 0原文

有人说你可以使用 get 与 Rebol 一起拥有(实现继承)。所以我尝试:

shape: context [
  x: 0
  y: 0
  draw: func['object][
    probe get object
  ]
]

circle: make shape [
  radius: 10
  draw: get in shape 'draw
]

rectangle: make shape [
  draw: get in shape 'draw
]

我想通过引用而不是值传递对象,所以我只使用 'Object.name' 传递名称。但是我必须这样称呼它,

circle/draw 'circle

这是相当蹩脚的,因为我需要重复名称循环两次,而在通常的继承中,有 this 关键字可以避免这种不自然的语法。有更优雅的方式吗?

谢谢。

Someone said you can have (implementation inheritance) with Rebol using get. So I tried:

shape: context [
  x: 0
  y: 0
  draw: func['object][
    probe get object
  ]
]

circle: make shape [
  radius: 10
  draw: get in shape 'draw
]

rectangle: make shape [
  draw: get in shape 'draw
]

I want to pass the object by reference not by value so I pass only the name using 'Object. But then I have to call it like this

circle/draw 'circle

which is rather lame as I need to repeat the name circle twice while in usual inheritance there is the this keyword which avoid this kind of unatural syntax. Is there a more elegant way ?

Thanks.

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

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

发布评论

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

评论(3

疏忽 2024-08-24 05:32:23

有一个self词。冒着对此产生错误的确定感的风险,我将给您一个示例,大概可以实现您想要的功能:

shape: make object! [
    x: 0
    y: 0
    draw: func [object [object!]] [
        probe object
    ]
]

circle: make shape [
    radius: 10
    draw: func [] [
        shape/draw self
    ]
] 

rectangle: make shape [
    draw: func [] [
        shape/draw self
    ]
]

调用基类函数来创建采用零参数的函数

这里,我们通过使用适当的“self” < b>当心:就像其他词一样,它会被绑定......并且绑定会粘住。一旦您开始使用抽象,这可能会变得棘手......

selfWordAlias: func [] [
    return 'self
]

triangle: make shape [
    draw: func [] [
        shape/draw get selfWordAlias
    ]
]

调用 triangle/draw 可能会让您感到惊讶。您在对象方法中,selfWordAlias 返回单词“self”。但是 self 的概念是在定义 selfWordAlias 时捕获并绑定的,它位于全局系统上下文中。这就是你得到的回报。

有一些工具可以处理这个问题,但请确保您牢牢掌握Rebol 中的范围界定

There is a self word. At the risk of creating a false sense of certainty about that, I'll give you an example that presumably does what you want:

shape: make object! [
    x: 0
    y: 0
    draw: func [object [object!]] [
        probe object
    ]
]

circle: make shape [
    radius: 10
    draw: func [] [
        shape/draw self
    ]
] 

rectangle: make shape [
    draw: func [] [
        shape/draw self
    ]
]

Here we've made functions that take zero arguments by calling the base class function with the appropriate "self"

Beware: like other words, it gets bound...and the binding sticks. This can get tricky once you start working with abstractions...

selfWordAlias: func [] [
    return 'self
]

triangle: make shape [
    draw: func [] [
        shape/draw get selfWordAlias
    ]
]

Calling triangle/draw will probably surprise you. You're in the object method and selfWordAlias returns the word "self". But the notion of self was captured and bound at the time the selfWordAlias was defined, which was in the global system context. So that's what you get back.

There are tools for dealing with this, but make sure you've got a firm grip on Scoping in Rebol !

爱,才寂寞 2024-08-24 05:32:23

我本以为使用 Rebol 的原型继承就足够了。它更简单、更优雅:

shape: make object!
  x: 0           
  y: 0
  draw: func [] [probe self]
]

circle: make shape [
  radius: 10
]

triangle: make shape []

给出以下结果:

>> shape/draw
make object! [
    x: 0
    y: 0
    draw: func [][probe self]
]
>> circle/draw            
make object! [
    x: 0
    y: 0
    draw: func [][probe self]
    radius: 10
]
>> triangle/draw          
make object! [
    x: 0
    y: 0
    draw: func [][probe self]
]

I would have thought that using Rebol's prototypical inheritance is all that you need. It is simpler and more elegant:

shape: make object!
  x: 0           
  y: 0
  draw: func [] [probe self]
]

circle: make shape [
  radius: 10
]

triangle: make shape []

Which give the following results:

>> shape/draw
make object! [
    x: 0
    y: 0
    draw: func [][probe self]
]
>> circle/draw            
make object! [
    x: 0
    y: 0
    draw: func [][probe self]
    radius: 10
]
>> triangle/draw          
make object! [
    x: 0
    y: 0
    draw: func [][probe self]
]
时光暖心i 2024-08-24 05:32:23

当我想对所有对象使用相同的函数(而不是函数的副本)时,我使用此方法。
每当您创建对象时,即使复制另一个对象,也指向该函数。

>> f: does [print "hello"]
>> o: context [a: 1 b: :f]
>> o/b
hello
>> p: context [a: 2 b: :f]
>> p/b
hello

>> same? get in o 'b get in p 'b
== true ;they are exactly the same function

>> append last second :f " world!" ;change the function
== "hello world!"
>> o/b
hello world!
>> p/b
hello world!

当然,您应该注意绑定问题。希望这会有所帮助。

I use this method when I want to use same function (not the copies of function) for all my objects.
Point the function whenever you create an object, even if you copy another object.

>> f: does [print "hello"]
>> o: context [a: 1 b: :f]
>> o/b
hello
>> p: context [a: 2 b: :f]
>> p/b
hello

>> same? get in o 'b get in p 'b
== true ;they are exactly the same function

>> append last second :f " world!" ;change the function
== "hello world!"
>> o/b
hello world!
>> p/b
hello world!

Ofcourse you should take care about the binding issues. Hope this will help.

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