On 08/08/2020 13:44, Andrea Faulds wrote:
For example, what if we allowed string prefixes to have special
user-defined meanings, like:
n"123_456" // expands to: gmp_init('123_456')
u"foo bar" // expands to: new UnicodeString("foo bar") (some class I
made up)
d"123.456" // expands to: new Decimal("123.456") (also an imaginary
class)
This would be a similar idea to JavaScript's template string tags:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
That reminds me more of C++'s "user-defined literals";
https://docs.microsoft.com/en-us/cpp/cpp/user-defined-literals-cpp?view=vs-2019
has a decent introduction, and https://stackoverflow.com/q/237804/157957
some discussion of their advantages. As you say, they are basically
sugar around a function call (implemented in C++ as an operator
overload, because that's a hammer they had available).
An interesting advantage in PHP would come if we could limit literals to
be computable at compile-time:
* They could be pre-computed and cached on compilation, saving a
function call.
* They could be used in restricted contexts such as constant definitions
and parameter defaults.
Initially, this would probably mean restricting them to built-in types,
but could potentially be opened up to userland later once some other
pieces were in place (structs feel closely related).
Regardless of the syntax, having a BigNum type constructible at
compile-time would be a big advantage over GMP, especially if it could
be generalised to expressions of multiple BigNums:
const GOOGOL = 10_bignum ** 100; // type of result inferred from one
operand, in the same way that 10*2 !== 10.0*2
const GOOGOLPLEX = 10 ** GOOGOL; // type of result inferred from
existing constant
function waste_time(BigNum $iterations = GOOGOLPLEX) { ... }
Regards,
--
Rowan Tommins (né Collins)
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php