« first day (4929 days earlier)      last day (33 days later) » 

12:40 AM
@bwoebi Are you racing to steal my syntax? :P
 
12:57 AM
@IluTov Not quite, but given that you used that syntax led me to talk again about macros, which led to me finding some motivation to actually draft that :-D
But yeah, syntax is just a detail in the end, whether macros or mutating data class functions use them, doesn't matter. I'd rather like to hear opinions on the functionality proposal and if anyone is interested in it / would like to talk to me about what macros should be able to do and how writing them can be made ergonomic in PHP :-)
 
 
10 hours later…
11:23 AM
@bwoebi The main issue I would see is that short of executing it to get the output, it would be entirely unparsable for the IDE.
 
11:57 AM
@MarkR Would it be … a problem to execute these? Macros should be mostly pure things, i.e. something a small wasm evaluator could evaluate.
 
12:54 PM
@bwoebi If it uses eval internally, then I'm not a huge fan because eval leaks memory because op_arrays are arena-allocated iirc.
 
1:37 PM
I can't even
PHP doesn't even know how to do rounding properly
 
2:37 PM
@Tiffany That one reminded me of a very old post of mine back from 2013: XPath Null Byte Injection in PHP. Wondering if password_hash(), password_verify() or both are affected.
 
@nielsdos top-level (I mean files and evals) op_arrays aren't arena allocated, only functions and classes
I was thinking of caching the op_arrays though, by default, unless it's annotated #[DynamicMacro] or such
so that the op_arrays would be persisted and the code not actually recompiled
Ideally it would do some dependency tracking, as in, which external dependencies are used. If these are all the same value, it would use the cached one.
But that's no easy feat I assume
(for reference @nielsdos heap.space/xref/php-src/Zend/… - emalloc() here.)
The primary challenges of what makes it expensive as is:
- evaluation of the macro code - string processing to build another string isn't too fast in PHP
- compilation of the generated code
- symbol tables are created upon call of include or eval
- a new stack frame is always created
The first two items are solved with good caching. The last two items would likely need creation of an op_array where the generated code is inlined.
I consider these performance problems solvable, but I would still like to know what the general perception of such a feature would be.
Because solving these problems is no quick task and I'd not like to invest too much time into something doomed to fail.
 
3:37 PM
My general perception: Yes, please.
 
4:17 PM
@bwoebi In its current form, are there any benefits of this over a normal function, wrapping the args in a string, and the callee invoking eval for you? I think macros are useful if they 1. offer new possibilities that weren't possible previously 2. they drastically simplify implementation (e.g. by performing parsing for you) 3. they are less overhead than manual parsing/eval.
 
4:34 PM
IMO if we're talking pre-processors in general, the most desirable thing in the short term is probably #ifdef
 
@IluTov There's little which macros will be able to do which you couldn't do already. I mean, there's been github.com/marcioAlmada/yay. It just has disadvantages in the sense of: no IDE support (macros as part of the language would most certainly have). 2. Yes, I think macro parsing needs to have tooling provided built-in, to make token processing easy (like this yay thing is able to). 3. Yes, it has to be less overhead, with control over the engine you can merge macros into op-arrays.
 
5:08 PM
@bwoebi Would it make sense to macros at compile time and make them return an AST (or a string I suppose) to be placed in the position of the macro call? That would make it 0 runtime overhead, and allow them to access things a normally invoked function cannot. Whether that's enough to justify the complexity, I don't know. Yay looks much more powerful still.
 
5:29 PM
@IluTov I don't think compile time macros are the solution. First problem obviously is the call stack of the compiler, it's not re-entrant. Second problem is that the AST is not safe to be manipulated. At least currently there's no schema for the AST at all. Compiler just assumes what parser gives it is valid. With user-manipulated AST that's going to be more interesting.
Regarding yay, ultimately you can do … just a macro to do the expression syntax yay provides, like yay!(here's your yay-compatible macro expression). We also may or may not opt to provide a similarly powerful macro expression syntax directly in the language. But that's just syntax sugar / APIs on top of a then existing macro support.
Also it leaves some abilities aside, not sure if we need them or such, but it'd allow polymorphic macros. I.e. $var->macro!(...); where the actually evaluated macro depends on $var. How bad or valuable that is, I currently cannot imagine.
 
5:55 PM
@bwoebi I don't like the idea of polymorphic macros. Macros are already fragile, and them depending on various inputs at runtime sounds extremely fragile.
@bwoebi Ok. I don't know how to solve that without a re-entrant compiler. A cached variant could solve that to a degree. But this is still something you could likely implement yourself, with eval and strings, without too much of a hassle.
 
@IluTov Oh, for sure. I mean, there are some projects which do that. But there are things which won't gain broad usage without some intrinsic language support. And it's as said also about performance.
@IluTov I'm not sure whether they'd be a good idea. May well be that they're not. Just saying there would technically be the possibility to.
@IluTov There are some things which macros which depend on inputs allow though. Let's say using the right escaping for the selected database diablect. e.g. a $pdo->query!() macro.
It's less about being actually dynamic with respect to changing inputs, e.g. with pdo you're likely targeting the same database engine at the specific call place with every call. However PHP itself doesn't know "oh this is going to call a mssql database". You know that as the code writer. The executing code doesn't inherently until it's actually executing it.
Or let's say, you have a library to do some data manipulation and a terse syntax to describe these independently of the underlying data structure. Macros could generate the right code to process that efficiently (given the context it knows which datastructure).
With a bit of caching a lot is possible to do efficiently.
But for sure, with everything … it's just a means to achieve things.
You certainly can also do hideous things with (polymorphic) macros, but that doesn't make it inherently bad.
 
 
4 hours later…
9:57 PM
@bwoebi I just wonder what the use case is then. If it isn't compile time, and it cannot control things in the current scope (which would give it one edge over functions) then it seems pretty limiting. It's useful for languages that don't have eval (or it would be significantly faster without eval), or that can check macro expansion at compile time. Custom DSL will never be well supported in IDEs, and for popular things like SQL, heredoc works ok (I still hate its indentation).
I also think that macros don't necessarily always need an eval. Wouldn't it be useful to have a DSL that you can turn into some other representation, without executing it first?
 
 
2 hours later…
11:46 PM
o/
 

« first day (4929 days earlier)      last day (33 days later) »