Drop the unused init@ field from classes with a self identifier

Abierto
#5,284 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
55/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
fsharp, javascript, python
Área
compilers

Línea de trabajo

Start by tracing how Fable represents class fields and constructor parameters in its AST, then follow the code that emits the init@ field for JavaScript and Python. Check the constructor handling described in #5235, including how precompiled classes signal a self identifier. Done means the unused field, parameter, argument, and assignment are omitted from generated output while that signal remains available.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

F# adds a hidden init@<line> field to a class whose constructor has a self identifier (type P() as this or new () as self). .NET uses it to throw InvalidOperationException when this is used before the object is fully initialised. The check is a FailInit() call inside the member bodies, so M throws when called from P's constructor:

type P() as this =
    let x = this.M()
    member _.M() = 1
    member _.X = x

Fable emits the field and sets it, but never reads it. The typed tree Fable gets from FCS doesn't contain the FailInit() check, so there is nothing to translate. On JS:

export class P {
    constructor() {
        const this$ = new FSharpRef(defaultOf());
        this$.contents = this;
        this.x = (P__M(this$.contents) | 0);
        this["init@3"] = 1;
    }
}

export function P__M(_) {
    return 1;
}

With an explicit constructor (#5235), the field is also a constructor parameter:

export class E {
    constructor(Count, init$00408$002D1) {
        this.Count = (Count | 0);
        this["init@8-1"] = (init$00408$002D1 | 0);
    }
}

export function E_$ctor_Z524259A4(a) {
    const self = new FSharpRef(defaultOf());
    let this$_1;
    const this$ = new E(a, 0);
    self.contents = this$;
    this$["init@8-1"] = 1;
    // ...
}

Python emits the same shape (self.init_004039 = init_004039).

Since Fable can't use the field, we could drop it from the output: the class fields, the constructor parameter, the argument in the record construction and the Set. That saves a property per object and a write per construction.

Something to check when doing it: #5235 uses the init@ field of a precompiled class to detect that its constructor has a self identifier, because precompiled libraries don't keep constructor bodies. Removing it from the Fable AST must not remove that signal.

Lenguaje dominante
F#
Estrellas
3.2k
Forks
335
Merge medio
19 h 39 min
PR fusionados (30 d)
158

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

  • Sin Dockerfile ni archivo de Docker Compose
  • Sin plantilla de pull request
  • Sin guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de fable-compiler/Fable

Todos los issues de fable-compiler/Fable

Issues similares

Más issues de Compilers

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.