[fpc-pascal] Feature proposal: function-based assignment operators

Sven Barth pascaldragon at googlemail.com
Fri Mar 29 10:40:49 CET 2013


On 29.03.2013 03:03, Xiangrong Fang wrote:
> 2013/3/29 Sven Barth <pascaldragon at googlemail.com
> <mailto:pascaldragon at googlemail.com>>
>
>     There was already a discussion some time ago whether we should allow
>     operators like "or=" and such as well and the result was simple: no.
>     I consider this the same here.
>
>     You can achieve a similar effect through type helpers in 2.7.1 though:
>
>
> Using type helper here may result in "apparently" same effect. But in
> fact they are very different.  The syntax OP proposed is a mechanism of
> the language (compiler), i.e. support will be ubiquitous, but if you
> implement it with type helper, then you have to write the helper (or use
> it) every time you need such very tiny "feature" (if not just save
> typing), which voids the whole idea of make the source code simpler.
> Unless you put such helper into the RTL, which will be similar to the
> OP's proposal, but not as elegant.

No, there is no difference, because even if we would implement support 
for "function based operators" we would not implement any default ones 
or only provide them with an additional unit. So you either need to 
implement them yourself or use an additional unit anyway.

> My opinion is that I like this feature, but I don't mind if it is
> implemented or not. That will indeed make the syntax more complex and a
> little un-pascal-ish.

A "little" is an understatement...

> As to the C-style += operator, I have one question, will it makes code
> slightly faster? because if you use Inc() or Dec() there will be a
> function call?  or, the compiler will always try to inline Inc or Dec?

No, "a += b" will be translated to "a := a + b" by the compiler (with 
all consequences like sideeffects etc.). The C-like operators are merely 
syntactic sugar.
"Inc" and "Dec" are normally implemented using compiler magic and thus 
are relatively fast, though the compiler will optimize statements like 
"a := a + 1" as well (though only if "a" is side effect free, so: no 
property for example, but that's also true for "Inc" and "Dec" :) ).
And as a sidenote: "Inc" and "Dec" can be overloaded as well.

Regards,
Sven




More information about the fpc-pascal mailing list