Skip to content

Infer type from property initialization expressions #4

Closed
@gohanlon

Description

@gohanlon

Description

MemberwiseInit currently needs you to explicitly specify the type for properties, even those that are initialized to expressions that contain sufficient type information, e.g. literal values. This manual type specification can feel redundant.

Swift's memberwise init can infer type from arbitrary expressions. Since literals are syntax-level, MemberwiseInit can at least infer types from most literal values. This would both enhance usability and more closely mirror Swift's memberwise init.

Some other expressions where type can be inferred from syntax are as Type expressions, and conditional/operator expressions that compose other expressions from which type can be inferred.

In the common case, instead of:

struct Example {
  var count: Int = 0
}

you could simply do:

struct Example {
  var count = 0
}

This type inference should support all Swift literals that contain sufficient type information, such as Int, Double, String, Bool, Array of literals, and Dictionary of literals, as well as any other expressions whose syntax provides clear type information.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions