0

I am trying to replicate the Verbal Expression library for python in Genie as an exercise to learn how to use classes. I've got simple classes like the ones listed at the tutorial down, however when it comes to instatiate methods in a object oriented fashion I am running into problems. Here is what I am trying:

For training purposes I want a method called add that behaves identically as string.concat().

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

But I get the error:

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

When I use a different approach, like:

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)

I get the error in the last line of the above text, with the warning:

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

I want to be able to make test.add(sec) behave identically to test.concat(sec), what can I do to achieve this?

lf_araujo
  • 1,991
  • 2
  • 16
  • 39

1 Answers1

1

a small example for this, Note: unhandle RegexError in

  1. new Regex
  2. r.replace

[indent=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
Zee
  • 78
  • 6
  • Thanks, Zee. One question, why the need for to_string there? – lf_araujo Jan 15 '16 at 09:53
  • Hi, If a class impl to_string method, it will support string interpolation. here `@"$(self)"` it is a string. But actually it was not needed. You can write something like this: `var join = string.joinv ("", s.data); var r = new Regex (join)`, or `var r = new Regex (string.joinv ("", s.data))`. – Zee Jan 15 '16 at 10:52