简体   繁体   English

模块名称是在绑定中使用的阴影全局名称

[英]module name is shadowing global name used in binding

tl;dr: How to change the following binding to be able to write Intl.DateTimeFormat.make() instead of Intl_.DateTimeFormat.make() ? tl; dr:如何更改以下绑定以能够编写Intl.DateTimeFormat.make()而不是Intl_.DateTimeFormat.make()

type dateTimeFormat;

[@bs.deriving abstract]
type formatOptions = {
  [@bs.optional]
  weekday: string,
  [@bs.optional]
  day: string,
  [@bs.optional]
  month: string,
};

module Intl_ {
    module DateTimeFormat {
      module Impl {
        type t;
      };

      [@bs.new] external make: unit => Impl.t = "Intl.DateTimeFormat";
      [@bs.send] external format: (Impl.t, Js.Date.t) => string = "";
    };
}

Intl_.DateTimeFormat.make()
  ->Intl_.DateTimeFormat.format(Js.Date.make())
  ->Js.log;

The issue问题

Without the underscore, this would compile to:如果没有下划线,这将编译为:

var Impl = /* module */[];

var DateTimeFormat = /* module */[/* Impl */Impl];

var Intl = /* module */[/* DateTimeFormat */DateTimeFormat];

console.log(new Intl.DateTimeFormat().format(new Date()));

exports.Intl = Intl;

The issue is that var Intl = ... shadows the global Intl , and thus breaks new Intl.DateTimeFormat() .问题是var Intl = ... new Intl.DateTimeFormat()了全局Intl ,从而破坏了new Intl.DateTimeFormat()

First of all, I'd consider this a bug in BuckleScript.首先,我认为这是 BuckleScript 中的一个错误。 This problem was most recently brought up in issue #3268 and partially resolved in this commit , but it still leaves a lot of names unreserved.这个问题最近在问题 #3268 中提出并在此提交中部分解决,但它仍然留下了很多未保留的名称。 You should consider bringing this up there or in a new issue.您应该考虑在那里或在新问题中提出这一点。

In the meantime, you can work around this by fully qualifying the name.同时,您可以通过完全限定名称来解决此问题。 Intl isn't actually a global object, but attached to the global object, which in the context of a web page is window . Intl实际上不是一个全局对象,而是附着全局对象,这在网页的背景是window But since JavaScript will look for names on the global object when it's not found in the local environment, it looks very much like a global name.但是由于 JavaScript 会在本地环境中找不到时在全局对象上查找名称,因此它看起来非常像一个全局名称。

So if you change make to:因此,如果您将make更改为:

[@bs.new] external make: unit => Impl.t = "window.Intl.DateTimeFormat";

it should work fine.它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM