tracker issue : CF-4058829

select a category, or use search below
(searches all categories and all time range)
Title:

Language/Syntax Enhancement - Array/Struct Literal append/update

| View in Tracker

Status/Resolution/Reason: To Fix//

Reporter/Name(from Bugbase): Abram Adams / Abram Adams (Abram Adams)

Created: 09/17/2015

Components: Language

Versions: 11.0

Failure Type: Unspecified

Found In Build/Fixed In Build: CF11_Final /

Priority/Frequency: Trivial / Unknown

Locale/System: ALL / Linux uBuntu 11.10

Vote Count: 6

Quite often I’m working with arrays and structs and have the need to either add or override keys/indexes to a struct/array.  To add a key, I can simply do struct["key"] = "something"; or struct.key = something.  However, there are many times when I’m adding/updating a lot of keys and have to do:

// initial struct
struct = { "a": 1, "b": 2 };
//...later
struct["key"] = "something";
struct["key2"] = "something else";
struct["key3"] = "something else";
struct["key4"] = "something else";
struct["a"] = 2;


Now, I know I can structAppend or arrayAppend (or the related member functions), but it would be really handy if I could use &= and += in arrays/structs so that I could inline updating or appending to these data types.  

In my mind the &= would be synonymous for structAppend() with the overwrite flag off and += would be with the overwrite flag on.  With these one would be able to turn the struct example above into:

// initial struct
struct = { "a": 1, "b": 2 };
//...later
struct &= { 
  "key": "something", 
  "key2": "something else", 
  "key3": "something else", 
  "key4": "something else",
  "a": 3
};


The resulting struct would be:

{
  "a": 1,
  "b": 2,
  "key": "something",
  "key2": "something else",
  "key3": "something else",
  "key4": "something else"
}

because &= behaves as overwrite=false. While the following would overwrite:

// initial struct
struct = { "a": 1, "b": 2 };
//...later
struct += { 
  "key": "something", 
  "key2": "something else", 
  "key3": "something else", 
  "key4": "something else",
  "a": 3
};

The resulting struct would be:

{
  "a": 3,
  "b": 2,
  "key": "something",
  "key2": "something else",
  "key3": "something else",
  "key4": "something else"
}

The arrayAppend version would treat &= as merge=false and += as merge=true.  Example:

// initial array
array = [1,2,3];
//...later
array &= [3,5,7];

This would end up with an array like:

[1,2,3,[3,5,7]]

With +=:

// initial array
array = [1,2,3];
//...later
array += [3,5,7];

It would end up with an array like:

[1,2,3,3,5,7]

If implemented, the + and & operators should also be able to perform the same actions in a immutable fashion.  For example:

// initial struct
struct = { "a": 1, "b": 2 };
//...later
combinedStruct = struct + { 
  "key": "something", 
  "key2": "something else", 
  "key3": "something else", 
  "key4": "something else",
  "a": 3
};

The resulting combinedStruct would be:

{
  "a": 3,
  "b": 2,
  "key": "something",
  "key2": "something else",
  "key3": "something else",
  "key4": "something else"
}

Likewise the & should be allowed, but in overwrite==false mode.  Example:

// initial struct
struct = { "a": 1, "b": 2 };
//...later
combinedStruct = struct & { 
  "key": "something", 
  "key2": "something else", 
  "key3": "something else", 
  "key4": "something else",
  "a": 3
};


The resulting combinedStruct would be:

{
  "a": 1,
  "b": 2,
  "key": "something",
  "key2": "something else",
  "key3": "something else",
  "key4": "something else"
}

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	4058829

External Customer Info:
External Company:  
External Customer Name: Abram Adams
External Customer Email:

Attachments:

Comments:

+1, excellent idea (Vote must be between 25 and 4000 characters)
Vote by External U.
5832 | September 17, 2015 12:37:53 PM GMT
Great idea to improve the expressiveness of the language!
Vote by External U.
5833 | September 17, 2015 12:43:20 PM GMT
This would be an excellent enhancement to the language to bring it in line with other modern languages
Vote by External U.
5834 | September 17, 2015 02:03:20 PM GMT
I've plus-voted this, but ti would be with two caveats: * For there to be a += operator, there first needs to be a + operator. * I'm not sold that the distinction Abram is making with &= works. A + operator makes sense to me; but I don't think the distinction between how + and & are intuitive or obvious. Still: not a reason to not have it, I guess.
Comment by External U.
5826 | September 18, 2015 02:43:11 AM GMT
Will be closing this bug as a duplicate since bug #CF-4054826 already exists. Will be adding immutable operations for + / & into the scope of the existing bug.
Comment by Immanuel N.
5827 | September 20, 2015 10:46:58 PM GMT
What is this CF-4054826 you speak of? I just get "The information requested is not found" when I browse to that.
Comment by External U.
5828 | September 21, 2015 12:14:14 AM GMT
Hi Immanuel, +1 to what Adam said. This ticket (which isn't PR-specific) should remain open since the public can't see/vote/track PR tickets. Thanks!, -Aaron
Comment by External U.
5829 | September 22, 2015 02:03:45 AM GMT
+1 ......................
Vote by External U.
5835 | September 22, 2015 02:04:06 AM GMT
Opened.
Comment by Rupesh K.
5830 | September 22, 2015 02:28:03 AM GMT
Thanks Rupesh
Comment by External U.
5831 | September 22, 2015 02:29:08 AM GMT
I like the idea, but " &= as merge=false and += as merge=true." Seems strange
Vote by External U.
5836 | March 16, 2016 04:40:53 PM GMT