타깃별로 다르게 하는 부분은, Gleam에서 함수 하나에 한 타깃용 external과 나머지 타깃용 Gleam 본문을 같이 둘 수 있어요. 맞는 external이 없는 곳에서는 본문이 실행돼요.
@external(javascript, "./int_ffi.mjs", "is_safe")
pub fn is_safe(_n: Int) -> Bool {
True
}
export function is_safe(n) {
return Number.isSafeInteger(n);
}
Erlang에서는 본문이 True를 돌려주는데, 거기서는 잃는 게 없으니 맞는 답이에요. JavaScript에서는 Number.isSafeInteger가 실제 숫자를 받아요. 이미 반올림된 값도 잡아내요. 한계를 넘은 값이 반올림으로 다시 안전 범위 안에 들어오는 일은 없거든요. 묻는 내용이 런타임에 관한 것이니 답이 그 런타임 쪽 파일에 있는 게 자연스러워요.
영어에서 번역·원문 보기For the per-target part, Gleam lets one function have an external for one target and a Gleam body for the rest. The body runs wherever no external matches.
@external(javascript, "./int_ffi.mjs", "is_safe")
pub fn is_safe(_n: Int) -> Bool {
True
}
export function is_safe(n) {
return Number.isSafeInteger(n);
}
On Erlang the body answers True, which is right there, since nothing is lost. On JavaScript Number.isSafeInteger gets the real number. It also catches a value that was already rounded, because a value past the limit never rounds back down into the safe range. The question is about the runtime, so it makes sense that the answer lives in the runtime's own file.