winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.02k stars 198 forks source link

Struct Declaration: Default Value #3121

Open staycoolcall911 opened 1 year ago

staycoolcall911 commented 1 year ago

The default value notation (= y) may appear in declarations of struct fields, class fields or function arguments.

The following sections of the language reference should mention the default value notation:

  1. 1.6 Optionality:

The default value notation (= y) in declarations of struct fields or function arguments will use this value if a value is not provided, and implies type is T (not T?).

  1. 1.6.1.1 Struct fields (under 1.6 optionality):

The default value notation (=) can also be used in struct declarations. If provided, the field is also not required in a struct literal definition, and the default value will be implied. It also means that the type of the field must be T and not T?, because we can ensure it has a value (in the example below the field radix as a type of num).

struct FormatOpts {
  radix: num = 10;
  someOptional: str?;
}

let opts = FormatOpts {};
assert(opts.radix == 10);
assert(opts.someOptional? == false); // <-- no value inside `someOptional`

A value can be omitted from a struct literal if the field is optional or if it has a default value in the struct declaration. If an optional field doesn't have a default value, its type must be T? (someOptional above). If it has a default value it's type must be T (radix above). This is a compilation error:

struct Test {
  hello: str? = "hello";
//       ^^^^ type should be `str` since a default value is provided
}

NOTE: Default values can only be serializable values (immutable primitives, collections of primitives or other serializable structs). This limitation exists because we will evaluate the expression of the default value only upon struct initialization (it is stored in the type system).

github-actions[bot] commented 1 year ago

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] commented 11 months ago

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!

staycoolcall911 commented 11 months ago

This issue originally had default value support for struct, function and class. I updated this issue to relate to struct only, while adding #4808 for function and #4807 for class

github-actions[bot] commented 7 months ago

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] commented 4 months ago

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!