On the topic of lfuns: - Would an lfun of `[..]= be feasible and/or (un)desirable? - There obviously is no `= lfun. Is that technically challenging or hasn't there simply been no need to make it possible?
I can imagine both operators to be useful in the context of String.Buffer: - `[..]= could replace a string in the middle of the buffer. - `= could be used as a shortcut to replace clear() and add().
On Thu, Aug 28, 2014 at 3:44 AM, Stephen R. van den Berg srb@cuci.nl wrote:
- There obviously is no `= lfun. Is that technically challenging or hasn't there simply been no need to make it possible?
Don't like this idea! When you assign to a name, it should always rebind. Imagine:
mixed foo = String.Buffer(); foo = "asdf";
Should that call foo->`=("asdf"), or reassign foo? IMO it should absolutely always reassign. Otherwise you get something like PHP's references - a pain to work with.
ChrisA
Chris Angelico wrote:
On Thu, Aug 28, 2014 at 3:44 AM, Stephen R. van den Berg srb@cuci.nl wrote:
- There obviously is no `= lfun. Is that technically challenging or hasn't there simply been no need to make it possible?
Don't like this idea! When you assign to a name, it should always rebind. Imagine:
mixed foo = String.Buffer(); foo = "asdf";
Should that call foo->`=("asdf"), or reassign foo? IMO it should absolutely always reassign. Otherwise you get something like PHP's references - a pain to work with.
The operation could be determined by "overloading" based on types. I.e. a String Buffer `=(string a) would only activate when the lvalue is a buffer type, and the rvalue is of type string.
On Thu, Aug 28, 2014 at 4:09 PM, Stephen R. van den Berg srb@cuci.nl wrote:
The operation could be determined by "overloading" based on types. I.e. a String Buffer `=(string a) would only activate when the lvalue is a buffer type, and the rvalue is of type string.
Maybe. Still strikes me as dangerously magical. I'd much rather mutation be done with methods (or augmented assignments).
ChrisA
Chris Angelico wrote:
On Thu, Aug 28, 2014 at 4:09 PM, Stephen R. van den Berg srb@cuci.nl wrote:
The operation could be determined by "overloading" based on types. I.e. a String Buffer `=(string a) would only activate when the lvalue is a buffer type, and the rvalue is of type string.
Maybe. Still strikes me as dangerously magical. I'd much rather mutation be done with methods (or augmented assignments).
As with all lfun-magic, it should be applied within reason, and thus should only be used if the result is intuitive to the reader. The whole point of lfuns is that people expect operators to behave in certain ways, so it is shorthand for named methods. Likewise in this case.
In case of a String.Buffer, if I have:
String.Buffer b="abcdef"; b+="ghi"; write(b); b="jkl"; b+="mno"; write(b);
Then it strikes me as "obvious" that this would result in printing abcdefghi and jklmno.
Excerpts from Stephen R. van den Berg's message of 2014-08-28 09:13:38 +0200:
String.Buffer b="abcdef"; b+="ghi"; write(b); b="jkl"; b+="mno"; write(b);
Then it strikes me as "obvious" that this would result in printing abcdefghi and jklmno.
what's not obvious is that b should still be of type String.Buffer.
greetings, martin.
Martin B??hr wrote:
Excerpts from Stephen R. van den Berg's message of 2014-08-28 09:13:38 +0200:
String.Buffer b="abcdef"; b+="ghi"; write(b); b="jkl"; b+="mno"; write(b);
Then it strikes me as "obvious" that this would result in printing abcdefghi and jklmno.
what's not obvious is that b should still be of type String.Buffer.
Isn't it? You can't change the type of something by assignment, only by declaration, which is done only once.
What if b is declared as "string|String.Buffer"? Is it "obvious" what should happen then?
Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
What if b is declared as "string|String.Buffer"? Is it "obvious" what should happen then?
string|String.Buffer is a union type, so using it in conjunction with an `= lfun should result in an error reported to the programmer.
string|String.Buffer is a union type, so using it in conjunction with an `= lfun should result in an error reported to the programmer.
That is not possible to know at compile time, however.
And we _never_ pass by value in pike, so it would be totally unexpected to any pike developer.
On Wed, Sep 17, 2014 at 10:57 PM, Stephen R. van den Berg srb@cuci.nl wrote:
Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
What if b is declared as "string|String.Buffer"? Is it "obvious" what should happen then?
string|String.Buffer is a union type, so using it in conjunction with an `= lfun should result in an error reported to the programmer.
class Foo { mixed `=(Foo other) {...} }
class Bar { }
int main() { string|Foo|Bar x=Foo(); x = Bar(); }
So this code would be illegal, then? What if x were declared as "mixed", or maybe as "object" instead of "string|Foo|Bar"?
Personally, I'm strongly against the idea of an assignment-replacement operator. Assignment to a member is governed by the container (with `[]= or `->=), and assignment to a simple name should just overwrite it. Anything else would be extremely confusing.
(Side point: With the actual `=() line commented out, the above code compiles and runs with no problems. But changing the declaration from "string|Foo|Bar" to just "Foo|Bar" results in a compilation error, "syntax error, unexpected TOK_IDENTIFIER, expecting TOK_LEX_EOF or ';'". It seems the parser's unable to handle piped declarations that don't start with a recognized name. I'd never noticed that till now, which I guess means it's not much of a limitation!)
ChrisA
It seems the parser's unable to handle piped declarations that don't start with a recognized name.
Yes, in places where an expression is allowed the "naked" pipe syntax is indistinguishable from a bitwise or expression, so it is not recognized as a type.
You can use object(Foo|Bar) to circumvent the problem.
I obviously didn't read all the message.
= normally would replace the contents of an lvalue. I think it would be very confusing to be able to block that.
pike-devel@lists.lysator.liu.se