Skip to content

How to refer to an instance variable with the same name as a property in a nested object #440

Answered by bioball
kennydo asked this question in Q&A
Discussion options

You must be logged in to vote

Typically what I do here is to add a local self = this.

import "@k8s/api/batch/v1/CronJob.pkl"

class Job {
    local self = this

    name: String

    function createResource(): CronJob = new {
        metadata {
            name = self.name
        }
    }
}

By the way, the function here isn't providing any value. You can just make that a fixed property. This is a little nicer because the value gets cached. Methods currently get re-computed every time they are called; Pkl does not memoize methods.

class Job {
    local self = this

    name: String

    fixed resource: CronJob = new {
        metadata {
            name = self.name
        }
    }
}

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@kennydo
Comment options

Comment options

You must be logged in to vote
2 replies
@kennydo
Comment options

@odenix
Comment options

Answer selected by kennydo
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants