Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return SELF by default when modified #202

Open
stdweird opened this issue May 8, 2018 · 7 comments
Open

Return SELF by default when modified #202

stdweird opened this issue May 8, 2018 · 7 comments

Comments

@stdweird
Copy link
Member

stdweird commented May 8, 2018

(i'm clueless how hard this is to implement, but oh boy is this annoying behaviour)

Currently

object template t;
"/a" = {
    SELF['b'] = 1;
};

gives

evaluation error [/tmp/t.pan:2.1-4.2]
existing element of type nlist cannot be replaced by one of type long
>>> call stack trace 
>>> [/tmp/t.pan:2.1-4.2]
>>> ====================

becuase the dml has missing SELF at the end.

i would propose that when SELF is modified in a dml block, that by default SELF is returned at the end instead of last assigned value (or whatever the current default behaviour is).

i guess noone modifies SELF and then returns something else, at least not intentionally.

@jouvin
Copy link
Contributor

jouvin commented May 8, 2018

I'm afraid it is breaking one basic principle in Pan: every statement returns a value and in a DML you return the value returned by the last statement executed. SELF['b'] = 1; evaluates to 1 which is the last value before the (implicit) return. I don't know Pan internals but I can imagine that this is quite an intrusive change... and may be something that can impact performance if before each implicit return you need to infer what produced the last value evaluated...

@stdweird
Copy link
Member Author

stdweird commented May 8, 2018

i should be a more clear: i would only modify the behaviour is the last statement was an assignment and if SELF was modified. i actually think that any dml block that modifies SELF and returns something else is very likely wrong; but i won't stand in the way of people trying to shoot themself in the foot 😄 .

@kwaegema
Copy link
Member

can we have a pan function assign(key,value), that does SELF[key] = value; SELF ?

@ned21
Copy link
Contributor

ned21 commented Oct 29, 2018

But why would you even write the code in the OP? Much better to just write:

"/a/b" = 1;

assign as a function, analogous to append is also a nice idea. Am curious as to how much I'd use it since it seems to me the most space-consuming boilerplate I seem to end up writing is:

"/a" = {
  if( SOMETHING ) {
    SELF['b'] = dict("foo", "bar", "x", "y');
  };
  if( is_defined(SELF) ) {
    SELF;
  } else {
    null;
  };
};

@kwaegema
Copy link
Member

In most use cases, we return SELF independent of existence.. so would then end up doing
"/a" = if (SOMETHING) assign('b', dict("foo", "bar", "x", "y')) else SELF;
or maybe even "/a" = assign_if(SOMETHING, 'b', dict("foo", "bar", "x", "y')))

@stdweird
Copy link
Member Author

@stdweird will try to implement it. if it is easy, will open a PR and rediscuss at next workshop . if complex, will close this issue

@ned21
Copy link
Contributor

ned21 commented Oct 29, 2018

"/a" = if (SOMETHING) assign('b', dict("foo", "bar", "x", "y')) else SELF;

will cause /a to be undef if SOMETHING is false, hence the extra acrobatics to check SELF and return SELF or null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants