glutinum-org / cli

https://glutinum.net/
59 stars 6 forks source link

"When" type constraint is used for return type #153

Open NatElkins opened 3 days ago

NatElkins commented 3 days ago

Issue created from Glutinum Tool

Glutinum version - 0.11.0-preview

TypeScript

    export class CompletionList<T extends CompletionItem = CompletionItem> {

        /**
         * This list is not complete. Further typing should result in recomputing
         * this list.
         */
        isIncomplete?: boolean;

        /**
         * The completion items.
         */
        items: T[];

        /**
         * Creates a new completion list.
         *
         * @param items The completion items.
         * @param isIncomplete The list is not complete.
         */
        constructor(items?: T[], isIncomplete?: boolean);
    }

FSharp

module rec Glutinum

open Fable.Core
open Fable.Core.JsInterop
open System

[<AbstractClass>]
[<Erase>]
type Exports =
    /// <summary>
    /// Creates a new completion list.
    /// </summary>
    /// <param name="items">
    /// The completion items.
    /// </param>
    /// <param name="isIncomplete">
    /// The list is not complete.
    /// </param>
    [<Import("CompletionList", "REPLACE_ME_WITH_MODULE_NAME"); EmitConstructor>]
    static member CompletionList<'T when 'T :> CompletionItem> (?items: ResizeArray<'T>, ?isIncomplete: bool) : CompletionList<'T when 'T :> CompletionItem> = nativeOnly

[<AllowNullLiteral>]
[<Interface>]
type CompletionList<'T when 'T :> CompletionItem> =
    /// <summary>
    /// This list is not complete. Further typing should result in recomputing
    /// this list.
    /// </summary>
    abstract member isIncomplete: bool option with get, set
    /// <summary>
    /// The completion items.
    /// </summary>
    abstract member items: ResizeArray<'T> with get, set

Problem description

Glutinum is creating some code that looks like this:

    static member CompletionList<'T when 'T :> CompletionItem> (?items: ResizeArray<'T>, ?isIncomplete: bool) : CompletionList<'T when 'T :> CompletionItem> = nativeOnly

which is invalid.

The snippet of Typescript is from @types/vscode. I am trying to create bindings so I can create VS Code extensions using Fable.

MangelMaxime commented 2 hours ago

Here there are 2 issues:

  1. CompletionItem is not a defined type so even at places where the syntax is valid we get an error. I think you didn't include CompletionItem type definition for the bug report so we should be able to ignore this problem.

  2. CompletionList<'T when 'T :> CompletionItem> seems to not a be valid F# like you pointed out. I suppose in F# when can't have the when rules applied for the return type.

    We could more investigation on the supported F# syntax to check if this is the only place where where is not supported.

This is the function responsible for generating that code:

https://github.com/glutinum-org/cli/blob/f0fafecf06ec129b5d06571eca9a12036d645bd3/src/Glutinum.Converter/Printer.fs#L166-L203

To fix this issue, we need to look on where it is called and if this is the correct function to call. Depending on the case, we can use another function or add a parameter to say if we want to generate the constraint or not.