Arc Forumnew | comments | leaders | submitlogin
(coerce 0 'num)
7 points by conanite 3046 days ago | 9 comments
Coercion being a popular topic these days, I spotted this yesterday -

  arc> (coerce 0 'num)
  Error: "Can't coerce 0 num"
  arc> (coerce 1 'num)
  Error: "Can't coerce 1 num"
In the particular case I'm dealing with, it's easy to work around, but I imagine this isn't the intended behaviour.


4 points by rntz 3045 days ago | link

A fix has been pushed to anarki, tagged as anarki.int-to-num.0. It's merged in the arc3.fixes branch. I've also modified it appropriately and merged it into rntz.coerce.0 to produce rntz.coerce.1 and merged that into arc3.master.

-----

5 points by pg 3045 days ago | link

Yes, this is a bug; fixed.

-----

1 point by rntz 3045 days ago | link

What would the intended result be? Just to get the same number back? Although intuitive, that would violate a fairly important invariant: that (coerce x ty) either errors or returns an object whose type is ty. 0.0? That seems a bit odd. I suppose 'exact->inexact would work.

-----

1 point by CatDancer 3045 days ago | link

Why is this invariant important?

-----

1 point by rntz 3045 days ago | link

It's more or less what coerce means.

-----

2 points by CatDancer 3045 days ago | link

In arc3 I can already ask 'coerce to convert something for me into some general type and get back a more specific subtype:

  arc> (type (coerce "0" 'num))
  int
Maybe that's ok.

-----

1 point by rntz 3045 days ago | link

Hm. I wasn't thinking of it in terms of sub- and super-types, but in that context it makes sense. Thinking this way, if 'int is a subtype of 'num, then (isa 2 'num) should ideally return t; unfortunately it's nil in the present arc. :( I guess we'll just have to chalk arc's handling of types up as a bit of leaky abstraction and leave it be. I'll go push a fix for (coerce 0 'num) to anarki.

-----

2 points by CatDancer 3045 days ago | link

  (def isa (x y)
    (or (is (type x) y)
        (and (is y 'num) (is (type x) 'int))))

  arc> (isa 34 'num)
  t
On the one hand, this seems like the "right" answer, on the other hand, so far I've haven't needed this.

-----

1 point by CatDancer 3046 days ago | link

ah, the

  ((eqv? type (ar-type x)) x)
line doesn't match because the ar-type is 'int

-----