Skip to content

Allow to combine bs.new with more attrs #2659

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

Closed
jchavarri opened this issue Mar 19, 2018 · 10 comments
Closed

Allow to combine bs.new with more attrs #2659

jchavarri opened this issue Mar 19, 2018 · 10 comments
Labels
discussion enhancement stale Old issues that went stale

Comments

@jchavarri
Copy link
Contributor

There was a conversation in Discord about some bindings to a JS library:

var grpc = require('grpc');
var hello_proto = grpc.load(PROTO_PATH).helloworld;
var client = new hello_proto.Greeter('localhost:50051',...);

Because bs.new can only be combined with bs.module (but in this case, it'd be needed with bs.send) I believe there is no way to express this in BuckleScript (or i havent' been able to find it).

cc @thangngoc89.

@bobzhang
Copy link
Member

bobzhang commented Mar 20, 2018

It is something we did not support yet: new a dynamic constructor.

I feel it is very hard to type it statically, any proposal is welcome

@bobzhang
Copy link
Member

bobzhang commented Mar 21, 2018

Some rough ideas:
introduce an abstract type, e.g, ('a0,'a1,'b)Js.constructor

external constructor1 : ('a0, 'b) Js.constructor -> 'a0  -> 'b = "#new"
external constructor2 : ('a0, 'a1, 'b) Js.constructor2  -> 'a0 -> 'a1 -> 'b = "#new"
external constructor3 : ('a0, 'a1, 'a2, 'b) Js.constructor3 -> 'a0 -> 'a1 -> 'a2  -> 'b = "#new"

@shinzui
Copy link

shinzui commented May 8, 2018

@bobzhang Is that something being considered for the future? I am trying to figure out the best way to use gRPC from Reason/OCaml.

@dvisztempacct
Copy link

I feel it is very hard to type it statically, any proposal is welcome

@bobzhang how does @bs.new currently affect the type of the binding? I would imagine that it doesn't affect it at all (you type it however you want when you declare it) and the @bs.new only affects how a Javascript function call is emitted by the back-end.

Perhaps I am misunderstanding...

@renovatorruler
Copy link

Is there any way by which this can be achieved using Bucklescript, howsoever hacky it may be?

var client = new hello_proto.Greeter('localhost:50051',...);

@yawaramin
Copy link
Contributor

A better solution in this particular case would be to write a gRPC compiler that targets OCaml/Reason. https://github.com/mransan/ocaml-protoc is a good head start but it doesn't support services ( mransan/ocaml-protoc#126 ).

@dvisztempacct
Copy link

@yawaramin that isn't a terrible idea, however I believe it's OT for this issue. At the time, it seemed like the right idea to provide bindings to grpc-node

@yawaramin
Copy link
Contributor

yawaramin commented Feb 4, 2019

This is a similar kind of dynamism to #2765 , we can do a similar hacky workaround for the time being. E.g. translating Javier's code,

/* Grpc.re */
type t;
[@bs.module "grpc"] external load: string => t;

/* Greeter.re */
let _helloProto = Grpc.load("/proto/path");
 /* ^ this... */

type t;
[@bs.new] external make: string => t = "_helloProto.helloworld.Greeter";
     /* ...needs to be the same as this ^ */

This again relies on BuckleScript not mangling names, and is unsafe, but no more so than [%%bs.raw] and the unsafety can be wrapped up in a module like above.

@ryb73
Copy link
Contributor

ryb73 commented Feb 12, 2019

This is what I ended up doing:

let newHack: a => b => t =
    [%bs.raw {|(Constructor, arg) => new Constructor(arg)|}];

Where a, b, and t are your constructor, argument, and output types, respectively. It'd be great to see a native solution for this. I'm also curious to see an answer to @dvisztempacct's question as I share the same confusion.

@stale
Copy link

stale bot commented May 3, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Old issues that went stale label May 3, 2023
@stale stale bot closed this as completed May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement stale Old issues that went stale
Projects
None yet
Development

No branches or pull requests

7 participants