Collection constructors (and lit)


#1

I have a PR to add empty_set and empty_array constructors. They take a type argument: hl.empty_array(hl.tint32).

I think I’d rather write hl.array([], type=hl.tint32). This is slightly tricky, what about hl.array(<set-of-int32>, type=hl.tdouble64? Does that do a per-element conversion, or throw an error? What about hl.array({1, 2, 3}, type=hl.tdouble64)?

Similarly I’d also like hl.lit(5, type=hl.tint32) (once we replace broadcast and capture with lit), where the type is optional. Should it also do type conversion when converting from Python to expressions? E.g. hl.lit(5, type=hl.tdouble64)? Does lit only take Python objects or does it also pass through Expressions?

Finally, I feel like it would be nice to have a non-*args array constructor a la Python [] in addition to the list-like conversion operator. Any suggestions on what to do call it? (Please, don’t say c.)


#2

I suspect we could make this work:

hl.array(tint32)()
hl.array(tint32)(1,2,3)
hl.array(1,2,3)

with:

def array(*args):
    if len(args) == 1:
        if (isinstance(args[0], Type)):
            return lambda *elements: typed_array_literal(args[0], list(elements))
        else:
            return typed_array_literal(args[0].dtype, [args[0]])
    else:
        return typed_array_literal(guess_type(args), args)