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

Static field initialization #1668

Open
yoav-steinberg opened this issue Feb 26, 2023 · 8 comments
Open

Static field initialization #1668

yoav-steinberg opened this issue Feb 26, 2023 · 8 comments
Labels
🛠️ compiler Compiler 🐶 dogfood Discovered while dogfooding Winglang

Comments

@yoav-steinberg
Copy link
Collaborator

yoav-steinberg commented Feb 26, 2023

We don't support static field initialization yet:

class R {
  static n: num = 5; // Error no support for initializing the static field
  static m: num; // Error uninitialized static fields don't make sense
}

We do support static fields in the type system, so when importing a JSII library with static fields it'll work. But we can't create our own static fields yet.

@staycoolcall911
Copy link
Contributor

We decided this is p2. See discussion here.

yoav-steinberg added a commit that referenced this issue Feb 28, 2023
See #107

also in this PR:
* de-dup diagnostics by storing them in an `IndexSet` instead of a
`Vec`.
* Added a new AST reference type `TypeMember` for referencing enum
fields and class statics. Currently the grammar has no way of
distinguishing between a nested reference and a type property reference.
So during type-checking I do a (unsafe) convert in the AST tree from one
type to the other. If we change the grammar to distinguish between the
two we can avoid the unsafe here.
* Changed `Reference::NestedReferece` to `Reference::InstanceMember` to
match nicely with the new `Reference::TypeMember`.

**Not in this PR**
  - Support static field initialization (#1668)
  - Support accessing preflight static stuff inflight (#1669)

*By submitting this pull request, I confirm that my contribution is made
under the terms of the [Monada Contribution
License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.

---------

Co-authored-by: Chris Rybicki <chrisr@monada.co>
@staycoolcall911 staycoolcall911 changed the title feat(compiler): static field initialization Static field initialization Mar 1, 2023
@github-actions
Copy link

github-actions bot commented May 1, 2023

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days.
Feel free to re-open this issue when there's an update or relevant information to be added.
Thanks!

@github-actions
Copy link

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days.
Feel free to re-open this issue when there's an update or relevant information to be added.
Thanks!

@eladb
Copy link
Contributor

eladb commented Aug 21, 2023

This would be great to support, but we should only allow this for constant data which is immutable data (e.g. primitives and immutable collections of primitives).

We should also support static immutable data in modules. Currently modules do not support top-level let statements (or any statements), but we should make a special case for this type of constants.

@Chriscbr fyi

@skyrpex
Copy link
Contributor

skyrpex commented Sep 20, 2023

We should also support static immutable data in modules. Currently modules do not support top-level let statements (or any statements), but we should make a special case for this type of constants.

Also functions? Imagine a nanoid62.w file:

bring util;
let alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* pub */ let nanoid62 = inflight () => {
  return util.nanoid(
    alphabet: alphabet,
    size: 22,
  );
};

@skorfmann
Copy link
Contributor

skorfmann commented Nov 12, 2023

looking for a way to reuse static immutable data in pre- and inflight. I think this would help here?

here's an eample in a module (config.w)

bring cloud;
bring aws;
bring "@cdktf/provider-aws" as tfaws;

pub class Config {
    // would be nice to share pre- and inflight, also as a static class attribute or something like this
    // see  https://github.com/winglang/wing/issues/1668
    pub static key(name: str): str {
        return "/wing-cloud/apps/config/staging/${name}";
    }

    pub static inflight keyInflight(name: str): str {
        return "/wing-cloud/apps/config/staging/${name}";
    }

    // can't do static methods yet, see
    // https://github.com/winglang/wing/issues/2583
    pub add(name: str, value: str) {
        new tfaws.ssmParameter.SsmParameter(
            name: Config.key(name),
            type: "String",
            value: value,
        ) as Config.key(name);
    }

    pub inflight get(name: str): str {
        return Config.fetchParameterValue(Config.keyInflight(name));
    }
  }

@skorfmann skorfmann added the 🐶 dogfood Discovered while dogfooding Winglang label Nov 12, 2023
@skorfmann
Copy link
Contributor

skorfmann commented Nov 13, 2023

went with attributes in this case which are both available pre- and inflight. edit: To be clear: I haven't found a fix, it's just a workaround.

class Foo {
a: str;

init() {
this.a = "foo"
}

where a is then available in preflight and inflight stages.

Copy link

github-actions bot commented May 7, 2024

Hi,

This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days.
Feel free to re-open this issue when there's an update or relevant information to be added.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🛠️ compiler Compiler 🐶 dogfood Discovered while dogfooding Winglang
Projects
Status: 🤝 Backlog - handoff to owners
Status: Todo - p2
Development

No branches or pull requests

5 participants