Fable icon indicating copy to clipboard operation
Fable copied to clipboard

Is there an option to keep the Unicode identifier in the code?

Open hlizard opened this issue 3 years ago • 4 comments

Description

Is there an option to keep the Unicode identifier in the code?

Repro code

module 模块测试
open System

let acc v0 v1 (a: float) = (v1 - v0) / a

type 车辆 (速度: float, a: float) =
    member 这.跑 参 = 参 / 速度
    member 这.起步() = acc 0.0 速度 a
    override 这.ToString() =
        System.String.Format("车辆速度{0} km/h", 速度)

let a=车辆(10, 2)
Console.WriteLine(a.ToString())
a.跑 5

Expected and actual results

actual results:

from __future__ import annotations
from fable_modules.fable_library.reflection import (TypeInfo, class_type)
from fable_modules.fable_library.string import format
from fable_modules.fable_library.types import to_string

def acc(v0: float, v1: float, a_1: float) -> float:
    return (v1 - v0) / a_1


def expr_0() -> TypeInfo:
    return class_type("模块测试.车辆", None, _8F66_8F86)


class _8F66_8F86:
    def __init__(self, _901F_5EA6: float, a_1: float) -> None:
        self._901F_5EA6 = _901F_5EA6
        self.a = a_1

    def __str__(self) -> str:
        _8FD9 : _8F66_8F86 = self
        return format("车辆速度{0} km/h", _8FD9._901F_5EA6)


_8F66_8F86_reflection = expr_0

def _8F66_8F86__ctor_7B00E9A0(_901F_5EA6: float, a_1: float) -> _8F66_8F86:
    return _8F66_8F86(_901F_5EA6, a_1)


def _8F66_8F86___8DD1_5E38073B(_8FD9: _8F66_8F86, _53C2: float) -> float:
    return _53C2 / _8FD9._901F_5EA6


def _8F66_8F86___8D77_6B65(_8FD9: _8F66_8F86) -> float:
    return acc(0, _8FD9._901F_5EA6, _8FD9.a)


a : _8F66_8F86 = _8F66_8F86__ctor_7B00E9A0(10, 2)

print(to_string(a))

_8F66_8F86___8DD1_5E38073B(a, 5)


expected results:

from __future__ import annotations
from fable_modules.fable_library.reflection import (TypeInfo, class_type)
from fable_modules.fable_library.string import format
from fable_modules.fable_library.types import to_string

def acc(v0: float, v1: float, a_1: float) -> float:
    return (v1 - v0) / a_1


def expr_0() -> TypeInfo:
    return class_type("模块测试.车辆", None, 车辆)


class 车辆:
    def __init__(self, 速度: float, a_1: float) -> None:
        self.速度 = 速度
        self.a = a_1

    def __str__(self) -> str:
        这 : 车辆 = self
        return format("车辆速度{0} km/h", 这.速度)


车辆_reflection = expr_0

def 车辆__ctor_7B00E9A0(速度: float, a_1: float) -> 车辆:
    return 车辆(速度, a_1)


def 车辆__跑_5E38073B(这: 车辆, 参: float) -> float:
    return 参 / 这.速度


def 车辆__起步(这: 车辆) -> float:
    return acc(0, 这.速度, 这.a)


a : 车辆 = 车辆__ctor_7B00E9A0(10, 2)

print(to_string(a))

车辆__跑_5E38073B(a, 5)


Related information

  • Fable version: 4.0.0-snake-island-alpha-012
  • Operating system: win10

hlizard avatar Jul 21 '22 01:07 hlizard

Should be possible to relax the identifier sanitization to allow unicode characters. Unicode identifiers already work fine when compiling to Rust, but it needs to be fixed for the other languages too.

++@alfonsogarciacaro

ncave avatar Jul 21 '22 02:07 ncave

If I'm not mistaken, except Rust, all languages are using the identifier sanitization that was designed for "old" JavaScript, so only ASCII characters are allowed. Ideally we should have different sanitization rules per language (particularly for the keywords) but if all target languages accept unicode we can remove the ASCII rule and just check the identifier doesn't start with a number and doesn't contain whitespace (which is possible in F# when using quotes): https://github.com/fable-compiler/Fable/blob/d5349d0e15ca31e48974e3c262555736ca1046ff/src/Fable.Transforms/Global/Prelude.fs#L166-L172

alfonsogarciacaro avatar Jul 21 '22 05:07 alfonsogarciacaro

Pinging @dbrattli as this seems to be for Python, although it can be applied to all languages.

alfonsogarciacaro avatar Jul 22 '22 01:07 alfonsogarciacaro

@alfonsogarciacaro I have now fixed this issue for Python

dbrattli avatar Aug 07 '22 06:08 dbrattli