acton icon indicating copy to clipboard operation
acton copied to clipboard

Missing '$IdentityOptG_new'

Open plajjan opened this issue 1 year ago • 4 comments

Acton Version

0.19.2.20240121.17.6.48

Steps to Reproduce and Observed Behavior

Try to check for None on a object member of optional type and we get an error.

class Foo(object):
    a: ?str
    b: ?str
    def __init__(self, a, b):
        self.a = a
        self.b = b


actor main(env):
    f = Foo("a", "b")
    if f.a is not None:
        pass

    env.exit(0)

we get this error:

/tmp/actonc-4dec71409d4b9763/out/types/test_none_in_class.c:107:36: error: call to undeclared function '$IdentityOptG_new'; ISO C99 and later do not support implicit function declarations

Expected Behavior

It should work!

plajjan avatar Feb 24 '24 21:02 plajjan

The workaround to this is fairly simple... but I think this should just work, right? @nordlander @sydow

plajjan avatar Feb 24 '24 21:02 plajjan

I thought it worked by some magic introduced somewhere in the compiler. Shouldn't it be better if we let opt(A) be defined in builtin.act so that we could defined whatever extension we want in the standard way (but continues to let the programmer write ?A to mean the same thing? The same thing goes for NoneType?

sydow avatar Feb 25 '24 15:02 sydow

I agree in principle, but there's a catch: we want ?A to be a base class of every class A, even those not yet defined! That requirement fits very badly with our class extension mechanism, which is why ?A has been treated as a rather odd (and buggy) special construct up to now.

Luckily, our expectations on ?A blend rather well with the idea of union types, where ?A can be defined as just a shorthand for the union None|A. And union types can also be made to support protocol extensions in a quite flexible way, based on the rule that T1|T2 implements protocol P if and only if both T1and T2 implement P.

For these reasons I'm inclined to see union types as the long term solution to whatever problems we currently have with the optional type. Especially so if we have a workaround that we can live with for a while.

nordlander avatar Feb 25 '24 21:02 nordlander

So the workaround is to introduce a local variable from the attribute of the object we want to inspect

actor main(env):
    f = Foo("a", "b")
    a = f.a
    if a is not None:
        pass

    env.exit(0)

I don't think it's urgent to fix this, since it's been a problem since all of eternity and we have this simple workaround... but it's annoying and I didn't want to loose track of it. Thus this issue. I think it can wait for unions, which should be just after integer subtyping :)

plajjan avatar Feb 25 '24 22:02 plajjan