Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Usage in Rescript #30

Open
ethanabrooks opened this issue Apr 21, 2021 · 5 comments
Open

Usage in Rescript #30

ethanabrooks opened this issue Apr 21, 2021 · 5 comments

Comments

@ethanabrooks
Copy link

HI,
I am a little new to Rescript. I am trying to reproduce the example in your README in Rescript. This is as far as I got:

module Option = {
  let let_ = Belt.Option.flatMap
}

type address = {street: option<string>};

type personalInfo = {address: option<address>};

type user = {info: option<personalInfo>};

// Get the user's street name from a bunch of nested options. If anything is
// None, return None.
let getStreet = (maybeUser: option<user>): option<string> => {
  let%Option user = maybeUser;
  // Notice that info isn't an option anymore once we use let%Option!
  let%Option info = user.info;
  let%Option address = info.address;
  let%Option street = address.street;
  Some(street->Js.String.toUpperCase);
};

I am getting:

  141 │   let%Option info = user.info;
  142 │   let%Option address = info.address;
->143 │   let%Option street = address.street;
  144 │   Some(street->Js.String.toUpperCase);
  145 │ };
  
  Did you forget a `=` here?

FAILED: cannot make progress due to previous errors.
>>>> Finish compiling(exit: 1)

I assume the let% syntax has to change somehow for rescript?

Thanks.

@rjhilgefort
Copy link

I'm seeing this same issue in ReScript. Is it possible to use this library with ReScript?

@ryyppy
Copy link
Contributor

ryyppy commented May 18, 2021

ReScript doesn't offer this kind of syntax, and we also don't recommend extending the language with arbitrary ppxes. bs-let introduces a lot of complexities that are also not desirable for the user.

We will eventually add async / await support for better promise handling, and for the option sugar above, it is more idiomatic to use a single pattern match on all your optional values instead.

Sorry for the inconvenience

@rjhilgefort
Copy link

ReScript doesn't offer this kind of syntax, and we also don't recommend extending the language with arbitrary ppxes. bs-let introduces a lot of complexities that are also not desirable for the user.

We will eventually add async / await support for more better promise handling, and or the option sugar above, it is more idiomatic to use a single pattern match on all your optional values instead.

Sorry for the inconvenience

Sounds good! Thank you for the reply and explanation!

@jihchi
Copy link

jihchi commented May 19, 2021

If you are keen on async story in ReScript, you may be interested in the post -- A proposal for async style sugar from ReScript Forum.

@kgtkr
Copy link

kgtkr commented Jun 13, 2021

It can be used, but it seems to have no advantages.

$ cat a.re            
let getStreet = (maybeUser: option(user)): option(string) => {
  let%Option user = maybeUser;
  // Notice that info isn't an option anymore once we use let%Option!
  let%Option info = user.info;
  let%Option address = info.address;
  let%Option street = address.street;
  Some(street->Js.String.toUpperCase);
};
$ bsc -format a.re
let getStreet = (maybeUser: option<user>): option<string> =>
  %Option({
    let user = maybeUser
    // Notice that info isn't an option anymore once we use let%Option!
    %Option({
      let info = user.info
      %Option({
        let address = info.address
        %Option({
          let street = address.street
          Some(street->Js.String.toUpperCase)
        })
      })
    })
  })

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants