jscl
jscl copied to clipboard
how to create javascript object? (i.e Float32Array)
I can't find way...sorry for simple question.
new Float32Array(10)
cl::new
seems just create empty object.
I think it is a perfect question, I am not sure but it doesn't seem to be possible yet. So it is a good opportunity to implement it.
My proposal would be a special form new
(renaming the existing new to something like make-object), and use it like (new <fn> <arg> ...)
, e.g: (new #j:Date 0)
.
new can't be an ordinary function
It doesn't compose expressions nicely. For instance, you can't use a function returning your constructor after new. Given:
function f () { this.x = 10; }
function g () { return f; }
this is not correct new (g())
, but var fn = g(); new fn()
would be.
Why isn't new (g())
correct? Seems working fine at least in chrome and
firefox. You can even pass parameters to f
with new (g())(x)
.
Andrea
On Fri, Apr 22, 2016 at 8:40 AM, David Vázquez Púa <[email protected]
wrote:
I think it is a perfect question, I am not sure but it doesn't seem to be possible yet. So it is a good opportunity to implement it.
My proposal would be a special form new (renaming the existing new to something like make-object), and use it like (new
...), e.g: (new #j:Date 0). new can't be an ordinary function
It doesn't compose expressions nicely. For instance, you can't use a function returning your constructor after new. Given:
function f () { this.x = 10; }function g () { return f; }
this is not correct new (g()), but var fn = g(); new fn() would be.
— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/davazp/jscl/issues/228#issuecomment-213288700
You are right. I don't know what I was thinking. It works :). My concern was that
(new (f X))
didn't seem a good syntax.
It would still need new to be a special operator to compile the function call differently.
Anyway, I think that
(new #'f x)
will work. It allow us to easily use any expression for the constructor and pass arguments. It can be implemented as a built-in function.
We still need to decide how it will interact with this in Lisp, in case we want to implement a constructor function in Lisp.
This branch https://github.com/davazp/jscl/compare/ffi-improvements contains code to be able to use cl::this from Lisp. It binds the variable in the prelude of every function, outside of any function introduced by the compiler.
I think that could work.