Declare and instantiate structs together

Something like this

	struct Settings {
		/** @brief Activates validation layers (and message output) when set to true */
		bool validation = false;
		/** @brief Set to true if fullscreen mode has been requested via command line */
		bool fullscreen = false;
		/** @brief Set to true if v-sync will be forced for the swapchain */
		bool vsync = false;
		/** @brief Enable UI overlay */
		bool overlay = false;
	} settings;

I'd like to have the possibility to type the same in Swift or something like

	let settings = struct Settings {
		/** @brief Activates validation layers (and message output) when set to true */
		bool validation = false
		/** @brief Set to true if fullscreen mode has been requested via command line */
		bool fullscreen = false
		/** @brief Set to true if v-sync will be forced for the swapchain */
		bool vsync = false
		/** @brief Enable UI overlay */
		bool overlay = false
	}()

I think other than less verbosity it could also have another sense: it'd make clearer that you want to use that struct only as a single var/let, no other use is supposed/allowed

Ps: of course the empty constructor would require default values for every member

1 Like

I don't think we can or should be able to express that (:point_left: personal preference). The type must be exposed with access control in some way in Swift.

In case of the struct I think this is the correct way to go, regardless the pitch:

private struct Settings {
  /** @brief Activates validation layers (and message output) when set to true */
  var validation = false
  /** @brief Set to true if fullscreen mode has been requested via command line */
  var fullscreen = false
  /** @brief Set to true if v-sync will be forced for the swapchain */
  var vsync = false
  /** @brief Enable UI overlay */
  var overlay = false
}
var settings = Settings()

If you want an anonymous type you can simply use a tuple.

var settings = (
  /** @brief Activates validation layers (and message output) when set to true */
  validation: false,
  /** @brief Set to true if fullscreen mode has been requested via command line */
  fullscreen: false,
  /** @brief Set to true if v-sync will be forced for the swapchain */
  vsync: false,
  /** @brief Enable UI overlay */
  overlay: false
)
7 Likes

I (personally, as is always implied) agree with @DevAndArtist. If a type is tightly coupled with the variable, it fits more to be tuple. If it needs to be referred to multiple times, it’d better be a separate declaration.

4 Likes