Fep Arrays
Disclaimer: This is not the way Fep arrays will work. Refer to the new article on Fep collections for more information.
In this article, I'll write a bit about how I think Fep arrays will look. Keep in mind this a work in progress and is by no means comprehensive. Without further ado...
In PHP, you define an array using the array()
construct. Fep will follow the lead of languages like JavaScript, Python, Ruby and Groovy and use the square bracket [] notation.
Initialization
For example, a simple array in PHP is creating like this:
$arr = array(1, 2, 3);
In Fep, this would be done like this:
arr = [1, 2, 3]
There are actually three things to notice here. First, Fep uses square brackets. Second, Fep has optional semicolons (similar to JavaScript and Scala). Third, Fep doesn't prefix all variables with $
. This should give your pinky finger a bit of rest and de-noise-ify your Fep code.
How about associative arrays? In this case, I don't want to deviate too much from PHP, so I'm going to go with the same syntax:
// PHP
$fruits = array(
"fruits" => array("a" => "orange", "b" => "banana",
"c" => "apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("first", 5 => "second", "third")
);
// Fep
fruits =
[
"fruits" => ["a" => "orange", "b" => "banana",
"c" => "apple"],
"numbers" => [1, 2, 3, 4, 5, 6],
"holes" => ["first", 5 => "second", "third")
]
Decomposition
What about that convenient list
construct? In Fep, we'll try again to reduce the typing and improve readability by introducing this syntax:
// PHP
list($a, $b, $c) = array("a", "b", "c");
// Fep
a, b, c = ["a", "b", "c"]
Accessors
To fetch a value from a Fep array, the regular PHP-style accessors will be used, with the main difference being that Fep will support negative indexing. That is, $array[-1] == $array[$array.length - 1]
.
arr = [1, 2, 3]
println $arr[0] // 1
println [4, 5, 6][1] // 5
println $arr[-1] // 3
mixed = ["a" => "b", "foo"]
println arr["a"] // b
println arr[0] // foo
API
All arrays in Fep will be Array
objects. As such, they'll have access to all the PHP array functions in an object-oriented way. Here's a few examples:
// PHP
$arr = array(1, 2, "x");
$len = count($arr);
array_push($arr, "y", "z");
in_array(1, $arr);
$merged = array_merge($arr, array("a", "b", "c"));
// Fep
arr = [1, 2, "x"]
len = arr.length
arr.push("y", "z")
arr.contains(1)
merged = arr + ["a", "b", "c"]