Skip to content

Improve editor guidance for XGo's implicit expression-to-closure conversions #3356

Description

@aofei

Problem

Some spx APIs accept a zero-argument callback because they need to evaluate an expression repeatedly. For example, waitUntil expects a condition of type func() bool.

XGo allows users to pass an expression directly:

waitUntil v.IsMature()

The compiler implicitly converts the expression into a closure, approximately equivalent to:

waitUntil => v.IsMature()

This conversion is not visible in the source code. XBuilder currently shows only the parameter-name inlay hint:

waitUntil condition: v.IsMature()

The condition: hint identifies the parameter, but it does not explain that the expression becomes a callback and may be evaluated multiple times.

A particularly confusing case is:

waitUntil v.IsMature

A user may write this accidentally after omitting (), but the code still compiles because v.IsMature is already a method value of type func() bool. It does not use the implicit expression-to-closure conversion.

Both forms are valid, but they rely on different language behavior. The editor currently provides no guidance about this distinction.

Possible improvement

Could xgolsw provide a dedicated inlay hint when XGo performs an implicit expression-to-closure conversion?

For example:

waitUntil condition: => v.IsMature()

Both condition: and => would be virtual editor hints.

The => hint could provide a tooltip such as:

XGo implicitly converts this expression to func() bool. waitUntil evaluates it each time it checks the condition.

The standard LSP textDocument/inlayHint capability appears suitable for this. An optional text edit could also allow users to make the conversion explicit by inserting => .

The hint should be based on resolved types rather than hard-coded API names.

Expected behavior:

  • waitUntil v.IsMature() shows the implicit-conversion hint
  • waitUntil ready shows the implicit-conversion hint
  • waitUntil => v.IsMature() does not show a redundant hint
  • waitUntil v.IsMature does not show the hint because it is already a method value
  • Existing function values do not show the hint
  • Valid method values are not reported as missing-parentheses errors

XBuilder may need to preserve and render the additional inlay hint and its tooltip when returned by xgolsw.

The waitUntil API documentation and teaching examples could also prefer the explicit form when introducing this behavior:

waitUntil => v.IsMature()

The goal is to make this non-obvious XGo conversion discoverable without treating valid code as an error or warning.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions