wing icon indicating copy to clipboard operation
wing copied to clipboard

Static field initialization

Open yoav-steinberg opened this issue 1 year ago • 8 comments

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.

yoav-steinberg avatar Feb 26 '23 19:02 yoav-steinberg

We decided this is p2. See discussion here.

staycoolcall911 avatar Feb 27 '23 08:02 staycoolcall911

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[bot] avatar May 01 '23 06:05 github-actions[bot]

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[bot] avatar Jul 12 '23 06:07 github-actions[bot]

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

eladb avatar Aug 21 '23 18:08 eladb

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,
  );
};

skyrpex avatar Sep 20 '23 07:09 skyrpex

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 avatar Nov 12 '23 21:11 skorfmann

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.

skorfmann avatar Nov 13 '23 10:11 skorfmann

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!

github-actions[bot] avatar May 07 '24 06:05 github-actions[bot]