How do you create array literals with HJScript or indeed HJavaScript?
In HJavaScript there is the Array
type, but I 开发者_如何学Gocan't see a way of constructing a literal that would translate, for example, to JS as [1,2,3]
. I don't want to have to create a new Array()
and then push items into it, if I don't have to.
Ideally I'm after a function like array :: [t] -> Array t
.
I could possibly use JConst
to implement array
, but it seems like a hack for something that should be straight-forward. I could also do the create-and-push method above to implement array
, this is also not great, though.
Here is array
by pushing; not so great.
array :: [Exp a] -> JS (JArray a)
array xs = do
arr <- new Array ()
mapM_ (`push` arr) xs
return arr
This question is the first I've heard of HJscript. Briefly looking at the docs, I can't see any way to make a simple array literal like [1,2,3]
. But, I do see a way to call functions, and note that [1,2,3] = Array(1,2,3)
. In fact, I'll bet interpreters treat the former as sugar for the latter. So if you can call functions, you can construct literals.
精彩评论