New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty Array Initialisers #692

Closed
DavePearce opened this Issue Nov 3, 2016 · 3 comments

Comments

Projects
1 participant
@DavePearce
Member

DavePearce commented Nov 3, 2016

The following fails to compile:

function f() -> int[]:
    return ""

The problem is here is twofold:

  1. String constants are encoded in WyIL using array constructors. Thus, "a" is encoded as ['a'], and "" is encoded as [].

  2. The type of the empty array constructor [] is void.

This is pretty unsatisfactory. For string constants, we can at least resolve the issue by introducing a special kind of string constant. For general array constants, it's not clear what the right solution is.

@DavePearce DavePearce added this to the ChangeList Pre v0.4.0 milestone Nov 3, 2016

@DavePearce DavePearce added Important and removed Optional labels Aug 3, 2017

@DavePearce DavePearce changed the title from Error with Empty Array / String Constants to Empty Array Initialisers Oct 2, 2017

@DavePearce

This comment has been minimized.

Show comment
Hide comment
@DavePearce

DavePearce Oct 2, 2017

Member

Empty strings now work fine. The only problem is empty array initialisers:

function f() -> int[]:
    return []

But, it does make me wonder whether we can revisit the issue of typing them as void[]. Consider this example:

function f(int[] items) -> (int r):
   //
   if x is void[]:
       return |x|
   else:
       // what is type of items here?
       return items[0] 

We can type items in the false branch as (int[]&!void[]). Previously, this type was not really usable. But, perhaps, with the new notion of readable array types it actually is. Curiously, this program does indeed type check:

type array is ((int[]&!(void[])) r)

function f(array xs) -> array:
    return [xs[0]]
Member

DavePearce commented Oct 2, 2017

Empty strings now work fine. The only problem is empty array initialisers:

function f() -> int[]:
    return []

But, it does make me wonder whether we can revisit the issue of typing them as void[]. Consider this example:

function f(int[] items) -> (int r):
   //
   if x is void[]:
       return |x|
   else:
       // what is type of items here?
       return items[0] 

We can type items in the false branch as (int[]&!void[]). Previously, this type was not really usable. But, perhaps, with the new notion of readable array types it actually is. Curiously, this program does indeed type check:

type array is ((int[]&!(void[])) r)

function f(array xs) -> array:
    return [xs[0]]

DavePearce added a commit that referenced this issue Oct 2, 2017

Support for typing empty array initialiser #692
This reverts the decision on permitting empty array initialiser (i.e.
"[]").  The reason for the original decision was simply that typing them
as void[] didn't work.  However, with the new infrastructure in place
for managing readable and writeable types it does, in fact, work.
@DavePearce

This comment has been minimized.

Show comment
Hide comment
@DavePearce

DavePearce Oct 2, 2017

Member

Woah ... that was easy!!!!!!

Member

DavePearce commented Oct 2, 2017

Woah ... that was easy!!!!!!

@DavePearce DavePearce closed this Oct 2, 2017

@DavePearce

This comment has been minimized.

Show comment
Hide comment
@DavePearce

DavePearce Oct 5, 2017

Member

Note, Z had some similar issues apparently according to Lindsay.

Member

DavePearce commented Oct 5, 2017

Note, Z had some similar issues apparently according to Lindsay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment