Repository navigation
fix: a string has contains, lowercase, uppercase, startsWith, endsWith, replace and split, and a List has flatMap and toList - #167
Merged
Conversation
…h, replace and split, and a List has flatMap and toList
Problem
A PHP# developer moving a Laravel app's code reached for everyday text and
list operations and found none. `text.contains("a")`, `text.lowercase()`,
`text.startsWith("a")`, `text.endsWith("a")`, `text.replace("a", "b")` and
`line.split(",")` were refused as a method on a non-object `string`,
`rows.flatMap(r => r)` as a method `List` does not have, and
`counts.values().toList()` too, since a `List` had no `toList`. The moved
code fell back on `global.str_contains` and the other PHP functions, which
spec section 8 says PHP# code never needs.
Solution
The standard library's `Sharp.Text.Text` extensions on `string` gain the
seven text members with Kotlin's names. Case changes run
`mb_strtolower` and `mb_strtoupper` in UTF-8, so `"ÉCOLE".lowercase()` is
`"école"`. `replace` replaces every match, `split` keeps the empty text
after a trailing delimiter, and an empty delimiter throws
`ValueError("Text.split: the delimiter is empty")`.
Each PHP function a member replaces is now refused through `global` and
bare, naming the member, as section 8 requires: `str_contains`,
`strtolower`, `mb_strtolower`, `strtoupper`, `mb_strtoupper`,
`str_starts_with`, `str_ends_with`, `str_replace` and `explode`. A
replacement row can name the argument the member runs on, so `explode`'s
refusal writes `line.split(…)` on its second argument and `str_replace`'s
writes `name.replace(…)` on its third.
`List<T>.flatMap` runs on the engine's `Sharp\Collection`, reading each
Iterable its function gives as a Sequence reads its source: a List, a Set
or a generator. A function that gives no Iterable throws TypeError.
`List<T>.toList()` lowers to the list itself, which PHP copies on a write,
so `toList()` now works on every Iterable: a List, a Set, a Map's values
and a lazy chain.
The engine fixtures that called the replaced functions through `global`
call the members, and their PHP twins call `mb_strtoupper` and
`mb_strtolower` as the inlined members do. Section 14.3's function value
example reads `global.ucfirst`, which the library does not replace.
php-sharp/
├── docs/spec.md section 18: text-methods; section 12: flat-map-to-list; section 14.3 reads global.ucfirst
├── ext/sharp/
│ ├── sharp.c Sharp\Collection::flatMap; sharp_sequence_append, shared with Sequence::toList
│ ├── sharp.stub.php flatMap
│ └── sharp_arginfo.h regenerated
├── sharp/composer/library/Sharp/Text/Text.sharp the seven text members
├── sharp/mago/crates/
│ ├── analyzer/src/expression/call/function_call.rs nine REPLACEMENTS rows; Replacement::MembersOn names the receiver's argument
│ ├── analyzer/tests/sharp.rs flatMap and toList types; the receiver's argument in a refusal
│ ├── names/src/lib.rs (List, toList) inlined
│ ├── prelude/assets/extensions/sharp.php ListMethods::flatMap and toList
│ └── sharp-bridge/{src,tests}/lower.rs toList lowers to the list
└── Zend/tests/sharp/
├── TextCalls.sharp, library_text.phpt a row per text member
├── library_text.inc new: the files both runs load
├── library_text_preload.phpt new: the text members under preloading
├── ListMembers/Shelf.sharp new: flatMap and toList
├── list_members.phpt new: the rows
├── list_members_preload.{inc,phpt} new: the rows under preloading
├── yield.phpt loads Sharp.Text.Text for split
└── Accessors/, Behaviors/, Constructors/, FunctionValues/, Import/, Overrides/, ParameterProperties/,
Preloaded/, Page, Suit the members in place of global.strtoupper, strtolower and explode
- flatMap over Maps gives their [key, value] entries: a collection method's function gives PHP#'s Iterable<R>, and a type argument a tuple fixes stays that tuple. - replace with empty text to replace throws ValueError, and both of its messages and split's name the member as the code writes it. - split takes Kotlin's limit, and Http splits a header line with it. - A Map has toList(), which gives its entries as List<[K, V]>. - An issue on an extension call names the member on the type it extends, as string.contains, and counts arguments from the first one inside (…). - An unknown method on a string, an int, a float or a bool lists the methods it has, and never says "non-object type". - flatMap's argument error shows its open type parameter as its bound. - The replaced PHP functions are gone from the test stubs, the slice fixture and the doc comments.
Section 19 gives a string compareTo, so the text-methods example no longer states it as a compile error, and the unknown-method rows use shuffled.
An extension on a Set, a Map, an Iterable and a union names its member on that type, a float lists its methods, and a receiver with no type, which no call reaches, is unreachable.
# Conflicts: # sharp/mago/crates/analyzer/src/invocation/arguments.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Validating PHP# on a real Laravel app showed that everyday text and list operations were missing.
text.contains("a"),text.lowercase(),text.uppercase(),text.startsWith("a"),text.endsWith("a"),text.replace("a", "b")andline.split(",")were refused as a method on a non-objectstring.rows.flatMap(r => r)was refused as a methodListdoes not have, and so wascounts.values().toList(). The moved code fell back onglobal.str_containsand the other PHP functions, which spec section 8 says PHP# code never needs.Solution
Sharp.Text.Text's extensions onstringgain the seven text members with Kotlin's names. Case changes run in UTF-8, so"ÉCOLE".lowercase()is"école".replacereplaces every match.splitkeeps the empty text after a trailing delimiter, and an empty delimiter throwsValueError("Text.split: the delimiter is empty").global, naming the member, as section 8 requires:str_contains,strtolower,mb_strtolower,strtoupper,mb_strtoupper,str_starts_with,str_ends_with,str_replaceandexplode.explode's refusal writesline.split(…)on its second argument, andstr_replace's writesname.replace(…)on its third.List<T>.flatMapruns on the engine'sSharp\Collection. It reads each Iterable its function gives as a Sequence reads its source: a List, a Set or a generator. A function that gives no Iterable throws TypeError.List<T>.toList()lowers to the list itself, sotoList()works on every Iterable: a List, a Set, a Map's values and a lazy chain.globalnow call the members. Section 14.3's function value example readsglobal.ucfirst, which the library does not replace.No file in
sharp/harness/changed.