Tcl Source Code

View Ticket
Login
Ticket UUID: b108e00ac8b1b6e449ea7843ea605ed27cc68c7c
Title: Defining const with a different value should raise an error
Type: Bug Created on: 2026-01-05 16:48:39
Submitter: apnadkarni Assigned to: nobody
Subsystem: - New Builtin Commands Severity: Minor
Priority: 5 Medium Last modified: 2026-06-19 04:57:58
Status: Open Closed by: nobody
Resolution: None Closed on:
Version:
Description:
In my opinion, the second line below should raise an error as the value being assigned does not match the existing value.

```
% const v 2
% const v 3; # Should raise an error
```
User Comments:
juliannoble2 added on 2026-01-11 01:35:41:
I would find const more useful if it raised an error in this case. Intuitively it's what I initially expected.

Tip 677's statement:

"The variable must not exist prior to const creating a constant variable (unless it is a constant variable; in that case, const silently does nothing)."

makes no mention of value checking in this case - so I assumed it was deliberate.

sebres added on 2026-06-18 17:23:54:

Short excerpt of a discussion from tclchat:

<sebres> shall not [const] with another value as previously throw an error? I mean:
<sebres>   % const A 5; const A 5; set A; #=> 5; # this is expected (cause can happen by source 2nd time), but...
<sebres> - % const A 5; const A 6; set A; #=> 5
<sebres> + % const A 5; const A 6; set A; #=> [e] can't change "A": variable is a constant
<sebres> imho, the former (-) is wrong, something like letter (+) shall happen instead
<apn> @sebres - see ticket [b108e00ac8]
<sebres> thx, Ashok... I'll try to fix it now
<apn> @dkf might want to opine
<apn> as to the rationale as I think the current behaviour is explicitly stated in the TIP
<sebres> then it is wrong anyway
<sebres> [set A 6] is prohibited, but "change" using [const] is allowed (however it's not changed)... either it shall be RO completely... or it must be changeable (with [const])
<sebres> doing as it would be changed successfully is simply wrong, no matter what TIP says about


apnadkarni added on 2026-06-19 04:57:58:
Further from the chat:


DonalFellows (via ischain) 2026-06-18 20:14:51 UTC – 2026-06-18 20:15:43 UTC

The problem is how to handle someone putting [const X [expr {...}] inside a loop.

I'm uncertain which way to go there.

apn (via ijchain) 2026-06-19 04:22:26 UTC – 2026-06-19 04:22:48 UTC

A loop does not introduce a new scope so I would say it should behave like anywhere else - const c $X; const c $Y should raise an error unless $X == $Y

Or to be more precise, $X eq $Y