hhvm / user-documentation

Documentation for those that use HHVM and write Hack code.
http://docs.hhvm.com/
Other
129 stars 159 forks source link

Built-in-types Arrays: vec/dict/keyset sections don't describe how to get values #849

Open yukonfb opened 4 years ago

yukonfb commented 4 years ago

Please complete the information below:

Where is the problem?

What is the problem?

Shows assignments, but not how to get values (!)


Please don't change anything below this point.


jjergus commented 4 years ago

Agreed, this definitely needs improvement.

How to get values from arrays ([], idx, maybe nested example as well)

This is currently documented in the subscript section but would probably make much more sense in the arrays guide like you suggested.

What will happen if key doesn't exist. Does it throw, does it return null, or something else?

I'm not sure if this is properly documented anywhere. Unfortunately this might also get pretty complicated (it's likely inconsistent between different array types, may depend on various INI options, and may keep changing as we migrate away from legacy PHP arrays), so it might be hard to keep up-to-date until things become more stable.

Ways to handle non-existent key (null-safe-like operations for [] and idx) $foo = mydict['badkey']['goodkey'] ?? null; // is it this?

This is documented in the coalesce section and also would probably make more sense in the arrays guide.

@Wilfred have you given any though to what we should do with the "Expressions and Operators" section? It looks to me like most of the pages should just be completely removed and their content moved to a more relevant place (e.g. the array stuff from above, "async blocks" somewhere in the async section, "yield" wherever we document generators, "new" wherever we document objects, etc.)

jjergus commented 4 years ago

There's also some documentation on the idx reference page: https://docs.hhvm.com/hack/reference/function/HH.idx/

That should probably just link to wherever we decide to document all this.

Wilfred commented 4 years ago

@jjergus agreed, I don't think the Expressions and Operators section is a very helpful way of structuring the content.

The structure I've been thinking of is:

Basic syntax
* Comments (3x syntaxes, doc comments)
* Strings (literals, interpolation)
* Numerics (literals, arithmetic)
* Booleans (literal, operations)
* Keywords (what they are, enforcement)

Statement syntax
* Conditionals
* Loops
* try/catch

Data structures (with recommendations)
* Hack arrays (vec, dict, keyset)
* Collections (Vector, Set, Map, ImmMap etc)
* PHP arrays (array, darray, varray, varray_or_darray)

Namespaces
* Declaring namespaces
* Aliasing namespaces

Async
* await and basic semantics
* concurrent blocks, async blocks

Generators
* Normal generators
* Async generators

Functions
* Named functions
* Anonymous functions

Object model
* Classes, methods, inheritance and visibility
* Constructors and parameter promotion
* Properties
* Interfaces
* Traits (semantics, constraints)
* User-defined attributes
* Class generics and associated type constants

Enums
* Inheritance
* Enforcement

Generics
* Erased generics (methods, classes, functions, inference)
* Reified generics

XHP
* Declare and using XHP
* Attributes and enums
* Namespace support

Indexes
* An operator index (e.g. ==>, =>, . etc)
* A built-in attribute index (__Override, __Newable etc)
* An HH_FIXME error code index

I think a thematic grouping is much easier to read and to find what you're looking for. Arrays are a big topic in Hack, so definitely deserve their own section.