objeck / objeck-lang

Objeck is a modern object-oriented programming language with functional features tailored for machine learning. It emphasizes expression, simplicity, portability, and scalability. The programming environment consists of a compiler, virtual machine, REPL shell, and command line debugger with IDE plugins.
https://objeck.org
Other
157 stars 11 forks source link

A question about `String->Replace()` #263

Closed ghost closed 1 year ago

ghost commented 1 year ago

It seems String->Replace() doesn't change the value of the string?

For example:

str := "example string";

str->Replace("example", "test");

str->PrintLine(); # still the original string

str := str->Replace("example", "test");

str->PrintLine(); # now it prints the changed string

Is it consistent with https://github.com/objeck/objeck-lang/issues/250?

objeck commented 1 year ago

Correct, it is immutable. It can be changed to modify the contents of the caller, thoughts?

ghost commented 1 year ago

Correct, it is immutable. It can be changed to modify the contents of the caller, thoughts?

Please have a look at this code:

class Test1 {
    function : Main(args : String[]) ~ Nil {
        str := "foo";
        str->Append("bar");
        str->PrintLine();
    }
}

If str->Append() could change the value of str, why can't str->Replace()?

Let we recall the failed to compile pieces of code on https://github.com/objeck/objeck-lang/issues/250:

class Test1 {
    function : Main(args : String[]) ~ Nil {
        str := "foo";
        (str + "bar")->PrintLine();
    }
}
class Test1 {
    function : Main(args : String[]) ~ Nil {
        str := "foo";
        str->Append("bar")->PrintLine();
    }
}

Then have a look at this code:

class Test1 {
    function : Main(args : String[]) ~ Nil {
        str := "foo";
        str->Replace("o", "a")->PrintLine();
    }
}

Surprisingly, it compiled just fine.

So my opinion is, there is nothing special about str->Replace().

If str->Append() doesn't return a new string, then so does str->Replace(). If you want to make str->Replace() return a new string, then you have to make str->Append() return a new string too.

objeck commented 1 year ago

Try

str += "bar";
str->PrintLine();

Append is called for the += operator. I did not want it to return a new instance for performance reasons. Let me think more about your feedback.

ghost commented 1 year ago

Try

str += "bar";
str->PrintLine();

Append is called for the += operator. I did not want it to return a new instance for performance reasons. Let me think more about your feedback.

Please don't misunderstand what I said. The only thing I said is that str->Replace() is not any more special than other methods, so if str->Append() doesn't return a new string, then str->Replace() shouldn't either.

ghost commented 1 year ago

str->ReplaceAll() shouldn't return a new string, too.

objeck commented 1 year ago

Thanks for the feedback. I break operations into two categories. The first is, operations that add to an existing string in a predictable manner, like stream concatenation. The second, operations that modify a buffer in a more dynamic manner such as substring replacement. Concatenation is a common operation used frequently to build strings. Operations that 'may' modify all the contents of an should be a mutable in my opinion.

ghost commented 1 year ago

Thanks for the feedback. I break operations into two categories. The first is, operations that add to an existing string in a predictable manner, like stream concatenation. The second, operations that modify a buffer in a more dynamic manner such as substring replacement. Concatenation is a common operation used frequently to build strings. Operations that 'may' modify all the contents of an should be a mutable in my opinion.

Any of what you said have been added to the document yet? I don't see any changes on Getting Started. Btw, long story short, nothing changed?

objeck commented 1 year ago

For the last release, the focus was fixing the REPL shell. I will revisit the API documentation this iteration and record a few videos. The API docs will link to code examples and videos.

For Getting Started, I added an example of how to iterate over a Map's values.

objeck commented 1 year ago

Below are trial run videos. I need to link the related code with the videos.

I will also post-edit the "real" videos once I finish creating the pipeline.

Do you think this is useful?