ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.55k stars 735 forks source link

[Improvement]: Remove adding type descriptor in the extract to constant code action #42041

Open LakshanWeerasinghe opened 4 months ago

LakshanWeerasinghe commented 4 months ago

Description

$subject

According to the spec when declaring constant it is optional to add the type descriptor. The current compiler has the capability to resolve the constant type without the type descriptor. Also when don't have the type-descriptor the compiler don't have to type check the expression against the constant expected type. Hence we can stop adding the type descriptor in the extract to constant code action.

https://ballerina.io/spec/lang/master/#section_8.8

module-const-decl :=
   metadata
   [public] const [type-descriptor] identifier = const-expr ;

Describe your problem(s)

No response

Describe your solution(s)

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

nipunayf commented 1 month ago

Bumping this issue as the style guide has been updated in compliant to the above changes, and the LS should provide solutions that reflect the best practices. https://github.com/ballerina-platform/ballerina-dev-website/pull/8884.

nipunayf commented 3 weeks ago

Simply removing the type symbol is not straightforward, as the code action may generate erroneous code in some scenarios. For instance, consider the following scenario, which generates an incompatible error.

function foo(decimal val) {}

// Extracts the following value
foo(12)

// Generates the following code
const CONST = 12 // which is inferred as int

The ideal solution would be to manually consider such cases and add the d prefix to such values. However, this imposes a performance bottleneck on the code action, and the scalability is also fragile since there can be more such cases once we expand the support for constants.