mirror of
https://github.com/apple/pkl.git
synced 2026-05-28 01:29:15 +02:00
Add support for const object members (#678)
This adds support for adding the `const` modifier to object members. Such object members are also required to have the `local` modifier applied. This follows SPICE-0011.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
module Birds2
|
||||
|
||||
abstract class Bird
|
||||
|
||||
bird: Bird
|
||||
@@ -0,0 +1,15 @@
|
||||
const baz = 15
|
||||
|
||||
foo {
|
||||
const local bar = baz
|
||||
|
||||
const local bar2 = biz()
|
||||
|
||||
const local function biz() = baz
|
||||
|
||||
qux = bar
|
||||
|
||||
corge = biz()
|
||||
|
||||
corge2 = bar2
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
amends ".../input-helper/modules/Birds2.pkl"
|
||||
|
||||
const local myBirdName = "Birdy"
|
||||
|
||||
local class MyBird extends Bird {
|
||||
name: String = myBirdName
|
||||
}
|
||||
|
||||
bird = new MyBird {}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
bar = 15
|
||||
|
||||
obj {
|
||||
const local function foo() = bar
|
||||
|
||||
res = foo()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
foo {
|
||||
res1 = 15
|
||||
const local qux = res1
|
||||
res2 = qux
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
baz = 15
|
||||
foo {
|
||||
qux = 15
|
||||
corge = 15
|
||||
corge2 = 15
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
bird {
|
||||
name = "Birdy"
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
–– Pkl Error ––
|
||||
Cannot reference property `bar` from here because it is not `const`.
|
||||
|
||||
x | const local function foo() = bar
|
||||
^^^
|
||||
at constLocalMethod#obj.foo (file:///$snippetsDir/input/errors/const/constLocalMethod.pkl)
|
||||
|
||||
To fix, do either of:
|
||||
1. Add modifier `const` to property `bar`
|
||||
2. Self-import this module, and reference this property from the import.
|
||||
|
||||
x | res = foo()
|
||||
^^^^^
|
||||
at constLocalMethod#obj.res (file:///$snippetsDir/input/errors/const/constLocalMethod.pkl)
|
||||
|
||||
xxx | text = renderer.renderDocument(value)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
at pkl.base#Module.output.text (pkl:base)
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
–– Pkl Error ––
|
||||
Cannot reference property `res1` from here because it is not `const`.
|
||||
|
||||
x | const local qux = res1
|
||||
^^^^
|
||||
at constLocalProperty#foo.qux (file:///$snippetsDir/input/errors/const/constLocalProperty.pkl)
|
||||
|
||||
To fix, do either of:
|
||||
1. Add modifier `const` to property `res1`
|
||||
2. Self-import this module, and reference this property from the import.
|
||||
|
||||
x | res2 = qux
|
||||
^^^
|
||||
at constLocalProperty#foo.res2 (file:///$snippetsDir/input/errors/const/constLocalProperty.pkl)
|
||||
|
||||
xxx | text = renderer.renderDocument(value)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
at pkl.base#Module.output.text (pkl:base)
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
–– Pkl Error ––
|
||||
Modifier `const` is not applicable to object members.
|
||||
Modifier `const` can only be applied to object members that are also `local`.
|
||||
|
||||
x | const foo = 10
|
||||
^^^^^
|
||||
|
||||
Reference in New Issue
Block a user