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

オープン
#5,284 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
55/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
fsharp, javascript, python
領域
compilers

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
F#
スター
3.2k
フォーク
335
平均マージ
19時間 39分
マージ済み PR(30日)
158

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートなし
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

fable-compiler/Fable のほかの issue

fable-compiler/Fable の issue をすべて見る

似ている issue

Compilers の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。