0

How do you get a char from a string. This returns a string

Js.String2.charAt("abc", 0) //=> returns "a" not 'a'

While this returns the character code (int)

String.get("abc", 0) //=> returns 97 not 'a'

The second one makes sense, if you look at the implementation

function get(s, i) {
  if (i >= s.length || i < 0) {
    throw {
          RE_EXN_ID: "Invalid_argument",
          _1: "index out of bounds",
          Error: new Error()
        };
  }
  return s.charCodeAt(i);
}

instead of reading the description

get(s, n) returns as a string the character at the given index number.

Rescript version 9

EDIT:

in javascript one could accomplish this with

 "abc".charAt(0) //=> returns 'a' 

and I could create the bindings for this, but I am surprised that I would need to. (given the libraries String and String2)

akaphenom
  • 6,728
  • 10
  • 59
  • 109
  • What exactly do you mean by `char`? And how do you think it differs from the character code? I suspect you're confusing the type with the representation of that type. – glennsl Apr 08 '22 at 08:28
  • @glennsl - its a good question I should have been more specfic in my post. By `char` I mean single byte ascii mapped representation. e.g, not 97, but 'a'; I am writing a parser that processes one character at a time, and coded everything to: `Char ReScript has a type for a string with a single letter:` https://rescript-lang.org/docs/manual/latest/primitive-types#char – akaphenom Apr 08 '22 at 11:48
  • There is no `char` data type in JavaScript. That's why `charAt` returns a string (in both JS and rescript), and the `char` type is represented as a `number` in JS. But even in C and other languages that have a "native" `char` type, it's still just represented as a number. It's just a matter of how you interpret that. If you look at `char` from JavaScript you'd just see a number, because JS doesn't know about `char`, but it should still work as you'd expect a `char` to work in rescript. – glennsl Apr 08 '22 at 12:01
  • So the question then is, what specifically is not working as you expect? – glennsl Apr 08 '22 at 12:02
  • updated question and proposed a heavy handed answer of wrrintg my own binding, that provides the proper results. Still there I think there must be a way for standard libraries to do this. – akaphenom Apr 08 '22 at 12:05

1 Answers1

0

One option is creating your own binding:

@send external charAt : (string,  int) => char = "charAt"

and use it

charAt("abc", 0) //=> 'a'

generates the JavaScript

"abc".charAt(0);
akaphenom
  • 6,728
  • 10
  • 59
  • 109
  • 1
    This is going to cause runtime errors when these values are used with other functions expecting `char`s because `charAt` returns a `string`, not a `char`. Because there is no such thing as JavaScript `char`. The correct (i.e. expected) representation for a `char` in rescript is a number, not a `string`. – glennsl Apr 08 '22 at 12:42
  • Again, it's not clear what behaviour doesn't work as you expect, since you don't provide a complete example, but I suspect it's just that it doesn't print the value as you expect in the console. If that is the case, and it's the only behaviour you need to work as you expect, why don't you just use the existing binding that correctly returns a `string` instead? Why do you need a `char`? – glennsl Apr 08 '22 at 12:44
  • I obviously could use a string, but I generally only returns a string of a length of 1. I didn't realize that Char was a number in practice (I am aware it has the code underlying it). Because of that I should change my approach to use a string... – akaphenom Apr 08 '22 at 12:49