Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Proposal for new language feature for the short hand property #1275

Open
patrycju opened this issue May 4, 2024 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed lang-design Issues or PRs related to kcl language design and KEPs

Comments

@patrycju
Copy link

patrycju commented May 4, 2024

Feature Request

Is your feature request related to a problem? Please describe:
When I'm working I like convenience and flexibility, and that's why I love KCL! I have an idea that I think will push that frontier a bit further.

Describe the feature you'd like:
Javascript is a pretty horrible language, but there is one cool feature that came out in ES6 - object property shorthand!
In short, when creating objects, we can use shorthand notation to assign variables as object properties with the same name.

// normal assignment
function createMessage (message, id) {
  return {
    message: message,
    id: id,
    timestamp: Date.now()
  }
}
// shorthand 
function createMessage (message, id) {
  return {
    message,
    id,
    timestamp: Date.now()
  }
}

In KCL, there could be shorthand assignment too, not only by variable name, but by type as well! Examples:

# let's define some basic schemas
schema CPUOptions:
    cores: int
    threads: int

schema DiskOptions:
    size: int
    $type: "sdd" | "hdd"

schema VirtualMachine:
    name: str
    cpu: CPUOptions
    disk: DiskOptions

# normal assignment
vm = VirtualMachine {
    name = "vm"
    cpu = CPUOptions { cores = 1, threads = 1}
    disk = DiskOptions { size = 32, $type = "ssd"}
}

# now lets add some matching names
name = "next_vm"
cores = 2
threads = 4
disk = DiskOptions { size = 8, $type = "hdd"}

# shorthand assignment by name
vm2 = VirtualMachine {
    name
    cpu = CPUOptions { cores, threads } # cores and threads also shorthand assigned by name
    disk
}

# shorthand assignment by type
vm3 = VirtualMachine {
    "last_vm"
    # cpu_options identifier is not necessary because VirtualMachine expects CPUOptions
    CPUOptions { cores = 4, threads = 8} # cores/threads cannot be shorthanded by type because they are both the same type and it's ambiguous
    # one DiskOptions is expected by VirtualMachine
    DiskOptions { 32, "ssd"} # + int and str can also omit identified
}

Describe alternatives you've considered:
N/A, I just thought this feature was really cool

Teachability, Documentation, Adoption, Migration Strategy:
As a bonus if that would work outside schemas and instances, that would be so convenient. Currently, I'm working on IaC tool and if I define e.g. one provider (like terraform provider) in a file, why should I add identifier to unique resource (I know it probably have very logical reason, but like I said, I like flexibility 😃)? But, when I need to add second instance, another shorthand instance cannot be added and I must use identifier.

Example:

Kubernetes {
  host = example.tdd
  context = rancher-asd
}

my_other_cluster = Kubernetes {
  host = localhost
  context = minikube
}

That's all from me, let me know what you think!

@Peefy Peefy changed the title Proposal for new language feature Proposal for new language feature for the short hand property May 5, 2024
@Peefy Peefy added enhancement New feature or request lang-design Issues or PRs related to kcl language design and KEPs labels May 5, 2024
@Peefy
Copy link
Contributor

Peefy commented May 5, 2024

Yes. Many languages like javascript and rust have this feature and syntax sugar. For KCL, it is also a good feature that can be implemented. 👍

@Peefy Peefy added the help wanted Extra attention is needed label May 5, 2024
@Peefy Peefy changed the title Proposal for new language feature for the short hand property [Enhancement] Proposal for new language feature for the short hand property May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed lang-design Issues or PRs related to kcl language design and KEPs
Projects
None yet
Development

No branches or pull requests

2 participants