# Feature-Request - Macros

- Thread: https://externals.io/message/26535
- Original archive: https://news-web.php.net/php.internals/26535
- Started by Mathias Bank on 2006-11-13T10:42:17+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #26535 — Mathias Bank — 2006-11-13T10:42:17+00:00

URL: https://externals.io/message/26535#26535

Hallo,

you know, that there was often a discussion about a "ifsetor" function. 
I want to generalize this discussion. If you know LISP, there is a 
fantastic macro function which allows you to generate own syntax (not a 
c macro function, which is really bad). It would be fantastic, if php 
could implement such a macro function. One example (for ifsetor): you 
could define

macro ifsetor($param1, $param2) {
    return "if (isset($param1)) return $param1; else return $param2;
}

use: $test = ifsetor($_GET["blubb"],null);

All you have to do is to not execute the parameters given in the macro 
parameter list. Instead, you have to evaluate the return statement and 
then to execute this statement.

In this way, all discussions about ifsetor (or anything like this) could 
be avoided.

The macro function could even be extended to allow new blocks, but then 
we have to extend the syntax to allow reference to the body. In this 
way, I always extend the syntax of the parameter data: only variables 
would be matched.

macro foreachkey($array as $key, :body $body) {
   return "foreach ($array as $key=>$__dev0) {
        $body
      }";
}

know, you can call
$secondArray = array();
foreachkey($myArray as $myKey) {
     $secondArray[] = $myKey;
}

this would be extended to
foreach ($myArray as $myKey=>$__dev0) {
   $secondArray[] = $myKey;
}

and this code will be executed.

As you see, with this macro functions, it could be achieved any 
individual syntax. It would be a pleasure, if php could support it. It 
could shorten a lot of code.

Mathias

---

## #26575 — Matt Sicker — 2006-11-14T19:53:59+00:00

URL: https://externals.io/message/26535#26575 · In reply to: #26535

> Hallo,
>
> you know, that there was often a discussion about a "ifsetor"
> function. I want to generalize this discussion. If you know LISP,
> there is a fantastic macro function which allows you to generate own
> syntax (not a c macro function, which is really bad). It would be
> fantastic, if php could implement such a macro function. One example
> (for ifsetor): you could define
>
> macro ifsetor($param1, $param2) {
>     return "if (isset($param1)) return $param1; else return $param2;
> }
>
> use: $test = ifsetor($_GET["blubb"],null);
>
> All you have to do is to not execute the parameters given in the
> macro parameter list. Instead, you have to evaluate the return
> statement and then to execute this statement.
>
> In this way, all discussions about ifsetor (or anything like this)
> could be avoided.
>
> The macro function could even be extended to allow new blocks, but
> then we have to extend the syntax to allow reference to the body. In
> this way, I always extend the syntax of the parameter data: only
> variables would be matched.
>
> macro foreachkey($array as $key, :body $body) {
>    return "foreach ($array as $key=>$__dev0) {
>         $body
>       }";
> }
>
> know, you can call
> $secondArray = array();
> foreachkey($myArray as $myKey) {
>      $secondArray[] = $myKey;
> }
>
> this would be extended to
> foreach ($myArray as $myKey=>$__dev0) {
>    $secondArray[] = $myKey;
> }
>
> and this code will be executed.
>
> As you see, with this macro functions, it could be achieved any
> individual syntax. It would be a pleasure, if php could support it.
> It could shorten a lot of code.
>
> Mathias
This could also help with writing code compatible across different major 
PHP versions (4, 5, and 6).  It could help fuel upgrades so that 
supporting PHP4 isn't such a hassle in the future.

I love the idea (especially the Lisp implementation).
-- 
Matt Sicker

---

## #26607 — Mathias Bank — 2006-11-15T17:06:42+00:00

URL: https://externals.io/message/26535#26607 · In reply to: #26535

Mathias Bank schrieb:
> Hallo,
> 
> you know, that there was often a discussion about a "ifsetor" function. 
> I want to generalize this discussion. If you know LISP, there is a 
> fantastic macro function which allows you to generate own syntax (not a 
> c macro function, which is really bad). It would be fantastic, if php 
> could implement such a macro function. One example (for ifsetor): you 
> could define
> 
> macro ifsetor($param1, $param2) {
>    return "if (isset($param1)) return $param1; else return $param2;
> }
> 
> use: $test = ifsetor($_GET["blubb"],null);
> 
> All you have to do is to not execute the parameters given in the macro 
> parameter list. Instead, you have to evaluate the return statement and 
> then to execute this statement.
> 
> In this way, all discussions about ifsetor (or anything like this) could 
> be avoided.
> 
> The macro function could even be extended to allow new blocks, but then 
> we have to extend the syntax to allow reference to the body. In this 
> way, I always extend the syntax of the parameter data: only variables 
> would be matched.
> 
> macro foreachkey($array as $key, :body $body) {
>   return "foreach ($array as $key=>$__dev0) {
>        $body
>      }";
> }
> 
> know, you can call
> $secondArray = array();
> foreachkey($myArray as $myKey) {
>     $secondArray[] = $myKey;
> }
> 
> this would be extended to
> foreach ($myArray as $myKey=>$__dev0) {
>   $secondArray[] = $myKey;
> }
> 
> and this code will be executed.
> 
> As you see, with this macro functions, it could be achieved any 
> individual syntax. It would be a pleasure, if php could support it. It 
> could shorten a lot of code.
> 
> Mathias

I have recieved a few mails which have shown, that some people 
misunderstood my idea. It seems, that most of you know c/c++ macros. But 
I don't want to implement these macro functionality, because this 
functionality is really poor. Have a look at the macro functionality of 
lisp: 
http://www.gigamonkeys.com/book/macros-standard-control-constructs.html 
and http://www.gigamonkeys.com/book/macros-defining-your-own.html. 
There, it is described, what macros in lisp can do. You will see, that 
such macros cannot be compared with c/c++ macros. But it would be a 
pleasure for php, if such macros would be possible. You should really 
read it, I couldn't believe it until I have realized it at my own.

PHP is a really great language and the syntax is much better than the 
syntax of lisp. But the macros are one big pleasure in lisp. And don't 
mistake lisp macros for c macros. They are much more powerful.

Mathias

---

## #26616 — Marcus Boerger — 2006-11-15T19:56:08+00:00

URL: https://externals.io/message/26535#26616 · In reply to: #26607

Hello Mathias,

  we would need a lexer/parser that support changing rules. That would mean
changing bothtools. Eventhough that might happen if there are other reasons
to do so,the toolswe change todo not necessarily support whatyou woule need.
Based on our current experience re2c and lemon would be a big step forward
as they also allow unicode handling. However i doubt that you can easily
allow changing rules in lemon.

Another solution would be to have your functionality as a stream operation.
That is an extension could rewrite the incoming script while it is being
compiled. I have however no idea if and how that could be achieved.

best regards
marcus

Wednesday, November 15, 2006, 6:06:42 PM, you wrote:

> Mathias Bank schrieb:
> > […]

> I have recieved a few mails which have shown, that some people 
> misunderstood my idea. It seems, that most of you know c/c++ macros. But 
> I don't want to implement these macro functionality, because this 
> functionality is really poor. Have a look at the macro functionality of 
> lisp: 
> http://www.gigamonkeys.com/book/macros-standard-control-constructs.html 
> and http://www.gigamonkeys.com/book/macros-defining-your-own.html. 
> There, it is described, what macros in lisp can do. You will see, that 
> such macros cannot be compared with c/c++ macros. But it would be a 
> pleasure for php, if such macros would be possible. You should really 
> read it, I couldn't believe it until I have realized it at my own.

> PHP is a really great language and the syntax is much better than the 
> syntax of lisp. But the macros are one big pleasure in lisp. And don't 
> mistake lisp macros for c macros. They are much more powerful.

> Mathias




Best regards,
 Marcus

---

## #26624 — Richard Lynch — 2006-11-15T20:51:25+00:00

URL: https://externals.io/message/26535#26624 · In reply to: #26535

> As you see, with this macro functions, it could be achieved any
> individual syntax. It would be a pleasure, if php could support it. It
> could shorten a lot of code.

It sounds like you could submit a patch and get it in... :-)

While you are at it, if you want to dump the java-like OOP and put in
all of CLOS, go for it :-) :-) :-)

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

---

## #26625 — Mathias Bank — 2006-11-15T20:59:48+00:00

URL: https://externals.io/message/26535#26625 · In reply to: #26624

Richard Lynch schrieb:
> 
> > […]
> 
> It sounds like you could submit a patch and get it in... :-)
> 
> While you are at it, if you want to dump the java-like OOP and put in
> all of CLOS, go for it :-) :-) :-)
> 

Well, I have never said, that this is easy. But instead of telling "no" 
to ideas like ifsetor operator (or something else), we could say: "well 
no: there will be a general solution". And this solution must not be 
invented, it "just" has to be integrated - we can look at lisp and how 
it is implemented there. Of course this is no job for a week or a month 
(so surely not for php 6). I published this feature request to discuss 
the idea with you php developers because you should know it best, if it 
is possible and if there are ways, to achieve this (and what are the ways).

So, there is no reason to be cynical.

Mathias

---

## #26671 — Richard Lynch — 2006-11-20T18:52:24+00:00

URL: https://externals.io/message/26535#26671 · In reply to: #26625

> Richard Lynch schrieb:
> > […]
>
> Well, I have never said, that this is easy. But instead of telling
> "no"
> to ideas like ifsetor operator (or something else), we could say:
> "well
> no: there will be a general solution". And this solution must not be
> invented, it "just" has to be integrated - we can look at lisp and how
> it is implemented there. Of course this is no job for a week or a
> month
> (so surely not for php 6). I published this feature request to discuss
> the idea with you php developers because you should know it best, if
> it
> is possible and if there are ways, to achieve this (and what are the
> ways).
>
> So, there is no reason to be cynical.

Cynicism was not intended at all.

Well, okay, the "all of CLOS" bit was maybe jokingly cynical.

But I'm absolutely serious that there seems to be support for the
lisp-like macros, so start working on a patch and submit it.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
