Re: Doubt about compiler error of `has_value` with arrays

Henrik Grubbström <[email protected]> Fri, 14 Mar 2025 14:32:56 +0100 (CET)
Newsgroups gmane.comp.lang.pike.user
Organization Roxen Internet Software AB
Message-ID <[email protected]>
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-1351141557-1741959176=:20628
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8BIT


Hi Marcos.

On Thu, 13 Mar 2025, Marcos Cruz wrote:

> I'm using Pike v8.0 release 1738 on Debian 12.9.
>
> I got this:
>
> ```
> int x;
> x = has_value(({ 1, 2, 3, 4 }), -100); // result: 0
> x = has_value(({ 1, 2, 3, 4 }), 0); // result: 0
> x = has_value(({ 1, 2, 3, 4 }), 4); // result: 1
> x = has_value(({ 1, 2, 3, 4 }), 5);
> Compiler Error: 1: Bad argument 2 to has_value.
> Compiler Error: 1: Expected: int(0..4).
> Compiler Error: 1: Got     : int(5..5).
> ```
>
> I expected a result of 0 also with numbers greater than 4, after the
> documentation
> (http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/has_value.html#has_value).
>
> Is it a bug or am I missing something?

This is an intended behavior. In Pike 9.0 and later the issue with
the first two not triggering a compilation error has been fixed.

| $ pike
| Pike v9.0 release 9 running Hilfe v3.5 (Incremental Pike Frontend)
| > has_value(({ 1, 2, 3, 4 }), -100);
| Compiler Error: 1: Bad argument 2 to has_value.
| Compiler Error: 1: Expected: int(1..4).
| Compiler Error: 1: Got     : int(-100).
| > has_value(({ 1, 2, 3, 4 }), 0);
| Compiler Error: 1: Bad argument 2 to has_value.
| Compiler Error: 1: Expected: int(1..4).
| Compiler Error: 1: Got     : zero.
| > has_value(({ 1, 2, 3, 4 }), 4);
| (1) Result: 1
| > has_value(({ 1, 2, 3, 4 }), 5);
| Compiler Error: 1: Bad argument 2 to has_value.
| Compiler Error: 1: Expected: int(1..4).
| Compiler Error: 1: Got     : int(5).

Note that the error is triggered because the compiler has sufficient
type information to determine that the respective calls will always
fail. This is typically not a problem except in interactive use
(ie hilfe), or eg when it is part of a macro. It is possible to
force the compiler to know less by eg casting to/via mixed:

| > has_value(({ 1, 2, 3, 4 }), (mixed)5);
| (2) Result: 0

Did this help?

 	/grubba

-- 
Henrik Grubbström					[email protected]
Roxen Internet Software AB				[email protected]
--8323328-1351141557-1741959176=:20628--