在rebol中有一个对象构造函数

时间:2013-10-07 15:56:42

标签: oop constructor rebol rebol2

我通常以“本能”的方式按功能编程,但我的当前问题可以通过对象轻松解决,所以我继续使用这种方法。

这样做,我试图找到一种方法来给对象一个构造函数方法,例如python中的 init ()。

我查看了http://www.rebol.com/docs/core-fr/fr-index.html文档,但找不到任何相关内容。

4 个答案:

答案 0 :(得分:6)

Rebol中没有特殊的构造函数,但如果您需要在 spec 块中创建对象,则可以编写 ad hoc 初始化代码。例如:

a: context [x: 123]

b: make a [
    y: x + 1
    x: 0
]

因此,如果您在基础对象中按惯例定义自己的“构造函数”函数,则可以在创建时将其称为spec块。如果你想让它自动化,你可以将它包装在一个函数中,如下所示:

a: context [
    x: 123
    init: func [n [integer!]][x: n]
]

new-a: func [n [integer!]][make a [init n]]

b: new-a 456

new-a 的更强大(但更长一点)的版本,可避免传递参数与 init 与对象自己的单词的可能冲突:

new-a: func [n [integer!] /local obj][
    also 
        obj: make a []
        obj/init n
]

您还可以编写一个更通用的 new 函数,该函数将基础对象作为第一个参数,并在克隆对象后自动调用构造函数,但支持可选的构造函数参数。通用的方式更加棘手。

记住,Rebol的对象模型是基于原型的(与基于类的Python和大多数其他OOP语言相比),因此“构造函数”函数得到重复为每个创建的新对象。如果要创建大量对象,可能需要避免此类费用。

答案 1 :(得分:5)

据我所知,没有使用init()之类的对象构造函数的正式方法/约定。当然有构建衍生对象的内置方法:

make prototype [name: "Foo" description: "Bar"]
    ; where type? prototype = object!

我最好的建议是定义一个检查构造函数方法对象的函数,然后应用该方法,这是一个我proposed previously的函数:

new: func [prototype [object!] args [block! none!]][
    prototype: make prototype [
        if in self 'new [
            case [
                function? :new [apply :new args]
                block? :new [apply func [args] :new [args]]
            ]
        ]
    ]
]

用法非常简单:如果原型对象具有new值,那么它将应用于衍生对象的构造中:

thing: context [
    name: description: none
    new: [name: args/1 description: args/2]
]

derivative: new thing ["Foo" "Bar"]

请注意,此方法适用于Rebol 2和3。

答案 2 :(得分:3)

实际上,再次阅读Rebol Core文档(我只是遵循了旧的建议:“阅读法语手册”),还有另一种实现构造函数的方法,非常简单:

http://www.rebol.com/docs/core-fr/fr-rebolcore-10.html#section-8

当然,它也出现在英文手册中:

http://www.rebol.com/docs/core23/rebolcore-10.html#section-7

=>

  

使用自变量的另一个例子是克隆的函数   本身:

person: make object! [
    name: days-old: none
    new: func [name' birthday] [
        make self [
            name: name'
            days-old: now/date - birthday
        ]
    ]
]

lulu: person/new "Lulu Ulu" 17-May-1980

print lulu/days-old
7366

我发现这很方便,这样,构造函数就在对象中。这一事实使对象更加自给自足。

我刚刚为一些地质事物成功地实施了它,并且运作良好:

>> source orientation
orientation: make object! [
    matrix: []
    north_reference: "Nm"
    plane_quadrant_dip: ""
    new: func [{Constructor, builds an orientation object! based on a measurement, as given by GeolPDA device, a rotation matrix represented by a suite of 9 values} m][
        make self [
            foreach [a b c] m [append/only matrix to-block reduce [a b c]] 
            a: self/matrix/1/1 
            b: self/matrix/1/2 
            c: self/matrix/1/3 
            d: self/matrix/2/1 
            e: self/matrix/2/2 
            f: self/matrix/2/3 
            g: self/matrix/3/1 
            h: self/matrix/3/2 
            i: self/matrix/3/3 
            plane_normal_vector: reduce [matrix/1/3 
                matrix/2/3 
                matrix/3/3
            ] 
            axis_vector: reduce [self/matrix/1/2 
                self/matrix/2/2 
                self/matrix/3/2
            ] 
            plane_downdip_azimuth: azimuth_vector plane_normal_vector 
            plane_direction: plane_downdip_azimuth - 90 
            if (plane_direction < 0) [plane_direction: plane_direction - 180] 
            plane_dip: arccosine (plane_normal_vector/3) 
            case [
                ((plane_downdip_azimuth > 315) or (plane_downdip_azimuth <= 45)) [plane_quadrant_dip: "N"] 
                ((plane_downdip_azimuth > 45) and (plane_downdip_azimuth <= 135)) [plane_quadrant_dip: "E"] 
                ((plane_downdip_azimuth > 135) and (plane_downdip_azimuth <= 225)) [plane_quadrant_dip: "S"] 
                ((plane_downdip_azimuth > 225) and (plane_downdip_azimuth <= 315)) [plane_quadrant_dip: "W"]
            ] 
            line_azimuth: azimuth_vector axis_vector 
            line_plunge: 90 - (arccosine (axis_vector/3))
        ]
    ]
    repr: func [][
        print rejoin ["Matrix: " tab self/matrix 
            newline 
            "Plane: " tab 
            north_reference to-string to-integer self/plane_direction "/" to-string to-integer self/plane_dip "/" self/plane_quadrant_dip 
            newline 
            "Line: " tab 
            rejoin [north_reference to-string to-integer self/line_azimuth "/" to-string to-integer self/line_plunge]
        ]
    ]
    trace_te: func [diagram [object!]][
        len_queue_t: 0.3 
        tmp: reduce [
            plane_normal_vector/1 / (square-root (((plane_normal_vector/1 ** 2) + (plane_normal_vector/2 ** 2)))) 
            plane_normal_vector/2 / (square-root (((plane_normal_vector/1 ** 2) + (plane_normal_vector/2 ** 2))))
        ] 
        O: [0 0] 
        A: reduce [- tmp/2 
            tmp/1
        ] 
        B: reduce [tmp/2 0 - tmp/1] 
        C: reduce [tmp/1 * len_queue_t 
            tmp/2 * len_queue_t
        ] 
        L: reduce [- axis_vector/1 0 - axis_vector/2] 
        append diagram/plot [pen black] 
        diagram/trace_line A B 
        diagram/trace_line O C 
        diagram/trace_line O L
    ]
]
>> o: orientation/new [0.375471 -0.866153 -0.32985 0.669867 0.499563 -0.549286 0.640547 -0.0147148 0.767778]
>> o/repr
Matrix:     0.375471 -0.866153 -0.32985 0.669867 0.499563 -0.549286 0.640547 -0.0147148 0.767778
Plane:  Nm120/39/S
Line:   Nm299/0

这种方式的另一个优点是由“new”方法定义的变量直接属于对象“实例”(我遇到了一些麻烦,其他方法,不得不提及自/有时,必须初始化变量或不是)。

答案 3 :(得分:3)

我试图了解OO如何在REBOL中运行。原型确实。昨天我遇到了这个页面,它启发了我下面的经典OO模型,没有重复功能:

;---- Generic function for class or instance method invocation ----;
invoke: func [
    obj  [object!]
    fun  [word!]
    args [block!]
][
    fun: bind fun obj/.class
    ;---- Class method names start with a dot and instance method names don't:
    unless "." = first to-string fun [args: join args obj]
    apply get fun args
]

;---- A class definition ----;
THIS-CLASS: context [
    .class: self                           ; the class refers to itself

    ;---- Class method: create new instance ----;
    .new: func [x' [integer!] /local obj] [
        obj: context [x: x' .class: none]  ; this is the object definition
        obj/.class: self/.class            ; the object will refer to the class
                                           ; it belongs to
        return obj
    ]

    ;---- An instance method (last argument must be the instance itself) ----;
    add: func [y obj] [
        return obj/x + y
    ]
]

然后你可以这样做:

;---- First instance, created from its class ----;
this-object: THIS-CLASS/.new 1
print invoke this-object 'add [2]

;---- Second instance, created from from a prototype ----;
that-object: this-object/.class/.new 2
print invoke that-object 'add [4]

;---- Third instance, created from from a prototype in another way ----;
yonder-object: invoke that-object '.new [3]
print invoke yonder-object 'add [6]

;---- Fourth instance, created from from a prototype in a silly way ----;
silly-object: yonder-object/.class/.class/.class/.class/.new 4
print silly-object/.class/add 8 silly-object
print this-object/.class/add 8 silly-object
print THIS-CLASS/add 8 silly-object

(它在REBOL 2中工作,并连续打印3,6,9,12,12,12。)几乎没有任何开销。可能很难找到十几种其他解决方案。究竟这是真正的问题:有太多的方法可以做到这一点。 (也许我们最好使用LoyalScript。)