[Issue 15692] Allow struct member initializer everywhere
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Tue Apr 5 18:44:08 PDT 2016
https://issues.dlang.org/show_bug.cgi?id=15692
Alex Parrill <initrd.gz at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |initrd.gz at gmail.com
--- Comment #1 from Alex Parrill <initrd.gz at gmail.com> ---
My use case: Vulkan uses structs to pass large amounts of parameters around. My
current code looks like this:
VkImageCreateInfo imgInfo = {
imageType: VkImageType.VK_IMAGE_TYPE_2D,
format: VkFormat.VK_FORMAT_R8G8B8A8_UNORM, // TODO: only allocate alpha if
needed
extent: image.size,
mipLevels: image.mipLevels,
arrayLayers: image.layers,
samples: VkSampleCountFlagBits.VK_SAMPLE_COUNT_1_BIT,
tiling: VkImageTiling.VK_IMAGE_TILING_LINEAR, //VK_IMAGE_TILING_OPTIMAL,
usage: VkImageUsageFlagBits.VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
VkImageUsageFlagBits.VK_IMAGE_USAGE_SAMPLED_BIT,
sharingMode: VkSharingMode.VK_SHARING_MODE_EXCLUSIVE,
initialLayout: VkImageLayout.VK_IMAGE_LAYOUT_PREINITIALIZED,
};
enforceVK(vkCreateImage(device, &imgInfo, null, &image.img));
It'd be really nice if it were possible to specify the struct inline with the
creation call:
auto img = createImage(device, VkImageCreateInfo {
imageType: VkImageType.VK_IMAGE_TYPE_2D,
// ...
initialLayout: VkImageLayout.VK_IMAGE_LAYOUT_PREINITIALIZED,
});
It'd also be very helpful for returning info structs from lamdas:
auto queueCreateInfo = priorities
.enumerate
.filter!(x => x[1].length > 0)
.map!((x) {
VkDeviceQueueCreateInfo info = {
queueFamilyIndex: x[0],
queueCount: cast(uint) x[1].length,
pQueuePriorities: x[1].ptr,
};
return info;
})
.array;
You also basically have to use the the named field initialization syntax
because each Vulkan info struct begins with a `sType` field and a `pNext`
field, which at the moment should be a struct-specific constant and null that
you don't really want to specify each time you create the struct.
--
More information about the Digitalmars-d-bugs
mailing list