类 在精灵中

Classes in Genie

我正在尝试在 Genie 中为 python 复制 Verbal Expression 库,作为学习如何使用 类 的练习。我有简单的 类 就像下面教程中列出的那样,但是当谈到以面向对象的方式实例化方法时,我 运行 遇到了问题。这是我正在尝试的:

出于培训目的,我想要一个名为 add 的方法,其行为与 string.concat() 相同。

[indent=4]
class VerEx : Object
    def add(strg:string) : string
        return this.concat(strg)

但是我得到错误:

verbalexpressions.gs:33.16-33.26: error: The name `concat' does not exist in the context of `VerEx'

当我使用不同的方法时,例如:

class VerEx : Object
def add(strg:string) : string
    a:string=""
    a.concat(strg)
    return a

init
    test:string = "test"
    sec:string = " + a bit more"
    print test.VerEx.add(sec)

我在上面文本的最后一行收到错误,并带有警告:

verbalexpressions.gs:84.11-84.21: error: The name `VerEx' does not exist in the context of `string?'

我希望能够使 test.add(秒) 的行为与 test.concat(秒) 的行为相同,我该怎么做才能实现这一点?

一个小例子, 笔记: 解除

中的 RegexError
  1. 新正则表达式
  2. r.replace

[缩进=4]

class VerX
    s: GenericArray of string

    construct ()
        s = new GenericArray of string

    def add (v: string): VerX
        s.add (v)
        return self

    def find (v: string): VerX
        return self.add ("(%s)".printf (v))

    def replace(old: string, repl: string): string
        // new Regex: unhandle RegexError HERE!!
        var r = new Regex (@"$(self)")
        // r.replace: unhandle RegexError HERE!!
        return r.replace(old, old.length, 0, repl)

    def to_string (): string
        return string.joinv ("", s.data)

init
    var replace_me = "Replace bird with a duck"
    var v = new VerX
    print v.find("bird").replace(replace_me, "duck")

    var result = (new VerX).find("red").replace("We have a red house", "blue")
    print result