Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there any reason incr is limited to integers?

120 views
Skip to first unread message

Georgios Petasis

unread,
Apr 4, 2018, 4:13:03 AM4/4/18
to
Hi all,

I have been wondering for some time now, why is incr limited to integers?

Is there a reason incr cannot handle real numbers?

I took a look at TclIncrObj() in tclExecute.c, and I didn't see a reason
against handling also real numbers.

Is it related to the byte compiler?

George

Christian Gollwitzer

unread,
Apr 4, 2018, 8:35:56 AM4/4/18
to
Am 04.04.18 um 10:12 schrieb Georgios Petasis:> I have been wondering
for some time now, why is incr limited to integers?
>
> Is there a reason incr cannot handle real numbers?

I think the reason is bugwarts compatibility. I used to define an "incf"
command which does "expr $arg+1" and it works fine for both integers and
floats. I think Donal Fellows prepares for this change because it makes
tclquadcode easier.

Christian

Donal K. Fellows

unread,
Apr 10, 2018, 5:05:15 PM4/10/18
to
On 04/04/2018 13:35, Christian Gollwitzer wrote:
> I think the reason is bugwarts compatibility.

Yes. There are scripts that use [incr someArgument 0] as a form of
integer argument type assertion.

Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.

Gerald Lester

unread,
Apr 10, 2018, 6:06:43 PM4/10/18
to
On 04/10/2018 04:05 PM, Donal K. Fellows wrote:
> On 04/04/2018 13:35, Christian Gollwitzer wrote:
>> I think the reason is bugwarts compatibility.
>
> Yes. There are scripts that use [incr someArgument 0] as a form of
> integer argument type assertion.

Ok, is there a good reason not to remove the restriction in 9.0?


--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

Arjen Markus

unread,
Apr 11, 2018, 2:37:28 AM4/11/18
to
On Tuesday, April 10, 2018 at 11:05:15 PM UTC+2, Donal K. Fellows wrote:
> On 04/04/2018 13:35, Christian Gollwitzer wrote:
> > I think the reason is bugwarts compatibility.
>
> Yes. There are scripts that use [incr someArgument 0] as a form of
> integer argument type assertion.
>

That could have a floating-point pendant like:

incr someArgument 0.0

Regards,

Arjen

Donal K. Fellows

unread,
Apr 11, 2018, 3:10:29 AM4/11/18
to
On 10/04/2018 23:06, Gerald Lester wrote:
> Ok, is there a good reason not to remove the restriction in 9.0?

Not really. We plan to have more explicit type assertions by then.
(They'll be regular Tcl commands that make semantic sense in any version
of Tcl, and could even be implemented using procedures.)

Christian Gollwitzer

unread,
Apr 11, 2018, 3:13:11 AM4/11/18
to
Am 11.04.18 um 08:37 schrieb Arjen Markus:
The point of Donal is that:

(chris) 53 % set a 3.5
3.5
(chris) 54 % incr a 0
expected integer but got "3.5"

will not throw an error in future Tcl, when incr accepts floats, since 0
can be added to both integers and floats. It would be much more
surprising if "incr" only incremented floats if the offset is also a float.

This has been apparently misused lazily as an argument checking
facility, instead of "if {[string is integer ...]}". A better way would
be type assertions, which also help for static compilation:

tcl::assert $a integer

is equivalent to

if {![string is integer $a]} {
error "Expected integer, got $a"
}

...or something along those lines.

Christian

Arjen Markus

unread,
Apr 11, 2018, 4:20:39 AM4/11/18
to
Ah, misunderstood the idiom as initialising a variable instead of a peculiar way of type checking.

Regards,

Arjen

Rich

unread,
Apr 11, 2018, 6:19:40 AM4/11/18
to
Christian Gollwitzer <auri...@gmx.de> wrote:
> Am 11.04.18 um 08:37 schrieb Arjen Markus:
>> On Tuesday, April 10, 2018 at 11:05:15 PM UTC+2, Donal K. Fellows wrote:
>>> On 04/04/2018 13:35, Christian Gollwitzer wrote:
>>>> I think the reason is bugwarts compatibility.
>>>
>>> Yes. There are scripts that use [incr someArgument 0] as a form of
>>> integer argument type assertion.
>>>
>>
>> That could have a floating-point pendant like:
>>
>> incr someArgument 0.0
>
> The point of Donal is that:
>
> (chris) 53 % set a 3.5
> 3.5
> (chris) 54 % incr a 0
> expected integer but got "3.5"
>
> will not throw an error in future Tcl, when incr accepts floats,
> since 0 can be added to both integers and floats. It would be much
> more surprising if "incr" only incremented floats if the offset is
> also a float.
>
> This has been apparently misused lazily as an argument checking
> facility, instead of "if {[string is integer ...]}".

When did 'incr x' appear in Tcl? When did 'string is integer' appear?

If 'incr x' for integers appeared before 'string is integer' arrived,
then it may have been the only way to test for 'integerness' of the
value of a variable at that time prior to the arrival of 'string is
integer'.

Georgios Petasis

unread,
Apr 11, 2018, 6:45:25 AM4/11/18
to
Στις 11/4/2018 00:05, ο Donal K. Fellows έγραψε:
> On 04/04/2018 13:35, Christian Gollwitzer wrote:
>> I think the reason is bugwarts compatibility.
>
> Yes. There are scripts that use [incr someArgument 0] as a form of
> integer argument type assertion.
>
> Donal.

Dear Donal,

Well, this is not a valid usage though. They should have used "string is
integer -strict". But I can understand what you mean :-)

The problem is not just incr, but everywhere we have incr (I am having
dict incr in mind). It is strange to have to write so many commands just
to increase a non integer quantity.

(I am having also usage issues with dict lappend. I know it has
inherited the semantics from lappend, where multiple values can be
appended to a variable. But in dict, I never have needed that. I need to
append a list kept in nested dicts, that can be indexed through a list
of keys instead...)

George

Georgios Petasis

unread,
Apr 11, 2018, 6:46:18 AM4/11/18
to
Στις 11/4/2018 10:10, ο Donal K. Fellows έγραψε:
> On 10/04/2018 23:06, Gerald Lester wrote:
>> Ok, is there a good reason not to remove the restriction in 9.0?
>
> Not really. We plan to have more explicit type assertions by then.
> (They'll be regular Tcl commands that make semantic sense in any version
> of Tcl, and could even be implemented using procedures.)
>
> Donal.

I also want incr to be fixed in 9.0 :-)

George

Donald Arseneau

unread,
Apr 11, 2018, 7:24:53 PM4/11/18
to
Rich <ri...@example.invalid> writes:

> When did 'incr x' appear in Tcl? When did 'string is integer' appear?
>
> If 'incr x' for integers appeared before 'string is integer' arrived,
> then it may have been the only way to test for 'integerness' of the
> value of a variable at that time prior to the arrival of 'string is
> integer'.

The better way has always been expr {$var|0}, which has worked forever.
Error messages are
can't use floating-point value as operand of "|"
and
can't use non-numeric string as operand of "|"


--
Donald Arseneau as...@triumf.ca
0 new messages