Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error when aliasing a Global value #3927

Open
joprice opened this issue Oct 14, 2024 · 1 comment
Open

error when aliasing a Global value #3927

joprice opened this issue Oct 14, 2024 · 1 comment

Comments

@joprice
Copy link
Contributor

joprice commented Oct 14, 2024

Description

Creating a top-level binding to a Global value with the same name causes a reference error

Uncaught ReferenceError: Cannot access 'self' before initialization

Repro code

open Fable.Core

type Exports =
    [<Global>]
    static member self: obj = nativeOnly
    static member x: int = 1

// this line generates: export const self = self;
let self = Exports.self
// this line generates: const self = self;
// let private  self = Exports.self

https://fable.io/repl/#?code=PYBwpgdgBAYghgIwDZgHQGFgCcwChcAuAnuFAKIAeI2BAzlALy5QtQDaAPAOJLAJxIAfAF1mrWgTgEAlgGMoAWzAKEYLFFpgkAMwBcUPgCtGUCFOkA3MAHkISImJYTz8pSrVQK+6RAImAjPgofpo6JpTUWHSoodq4wVAA+rHhVDS0MVraQA&html=Q&css=Q

Expected and actual results

Ideally, this situation would either be a compiler error or create an shadowed aliased value. In local scopes, a similar reference is aliased locally:

let y () = 
  let self = Exports.self
  self
export function y() {
    const self_1 = self;
    return self_1;
}

For public toplevel fields, which are automatically exported, I'm not sure how it should behave.

Related information

  • Fable version: 4.22.0
  • Operating system OSX
@MangelMaxime
Copy link
Member

I don't think there is something we can do about this one because we can't garante that there is no self variable available in the runtime.

I suppose we could argue that const self = self will always fails in JavaScript because of Uncaught SyntaxError: Identifier 'self' has already been declared.

And that let self = self can sometimes works in JavaScript, if self is available globally before.

For me this issue, ask Fable to do more than verifying F# and lean toward JavaScript/TypeScript compiler/runtimes responsibility.

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

No branches or pull requests

2 participants