Ticket #2178 (new bug)

Opened 10 years ago

Last modified 10 years ago

Parrot lacks a boolean type

Reported by: lucian Owned by:
Priority: normal Milestone:
Component: core Version: 3.6.0
Severity: medium Keywords:
Cc: Language:
Patch status: Platform:

Description

And instead, boolean contexts fall back to expecting ints. This is not semantically correct, and leads to issues, especially when trying to expose a HLL's object system to the rest of parrot.

In my specific case, I implemented get_bool on Python objects, but Rosella's is_true and winxed's if coerce to int. Rosella and Winxed are doing the least wrong thing possible with that they've got.

Implementing get_integer to return 0 for false, 1 for true and the value for Python ints is simply incorrect. At least in python 0 is falsy, which makes it possible to have this workaround, in other languages it would be required to simply not implement get_bool or get_integer, to not break things.

The workaround in Winxed is the ternary operator (bla ? 1 : 0), which ends up calling bla.get_bool.

Change History

Changed 10 years ago by jkeenan

  • component changed from none to core

Changed 10 years ago by pmichaud

On Tue, Aug 16, 2011 at 12:36:55PM -0000, Parrot wrote:
> #2178: Parrot lacks a boolean type
>  And instead, boolean contexts fall back to expecting ints. This is not
>  semantically correct, and leads to issues, especially when trying to
>  expose a HLL's object system to the rest of parrot.

I don't understand this ticket.  Parrot *has* a boolean type, both in 
the "Boolean" data type, and in the "get_bool" vtable function (which 
is separate from "get_integer").  I'm not aware of Parrot requiring
"boolean contexts fall back to expecting ints", nor does it do so in
many cases.

It's true that the get_bool vtable expects to return an integer
indicating true/false, but it's still a very separate vtable from
get_integer.  Also, most (if not all) of the boolean opcodes use get_bool
and not get_integer:

    if $P0, label
    unless $P0, label
    and $P0, $P1, $P2
    or $P0, $P1, $P2
    not $P0, $P1
    istrue $I0, $P0
    isfalse $I0, $P0

That said, it wouldn't bug me if we had a get_bool_pmc vtable to
go along with get_bool.

>  In my specific case, I implemented get_bool on Python objects, but
>  Rosella's is_true and winxed's if coerce to int. 

That sounds like a HLL/library issue to me, not a Parrot one.

>  Rosella and Winxed are
>  doing the least wrong thing possible with that they've got.

Note that none of NQP, Rakudo, nor PCT perform casting to ints in
order to provide boolean context.

Pm

Changed 10 years ago by NotFound

One problem in the parrot side is the lack of a way to call vtable set_bool from pir.

On the winxed side, I think the problem lies in the && and operators, not in if or other conditionals.

Note: See TracTickets for help on using tickets.