grid icon indicating copy to clipboard operation
grid copied to clipboard

Initialization without Default trait requirements

Open hornyjailfish opened this issue 2 years ago • 3 comments

I cant initialize grid with defined size because struct i use not impl Default I dont understand bound to Default trait because its possible to create vec and even Vec<Vec<T>> with Vec::with_capacity() function

hornyjailfish avatar Mar 24 '23 14:03 hornyjailfish

@hornyjailfish could you explain a bit what you try to achive? new does depend on the 'Default' trait right now because it will init a 2d grid with the default values of the given type. What would be your expectation on calling new(1,1) on a grid type without a Default trait bound?

Or in general, what can be achived with Vec<Vec<>> that can't be done with grid?

becheran avatar Mar 24 '23 15:03 becheran

want to create 2d grid for game with bevy like struct Field { ids: Grid<(Entity, Status)> } because Entity from the crate i cant impl Default for it but i want to init grid beforehand. I resolved it with dumb value and fill() and its ok for my case but could be hard for complex structs without Default

hornyjailfish avatar Mar 26 '23 18:03 hornyjailfish

I could imagine that something like grid_with_capacity would help in such case? It does not exist yet. Changing new is not that easy, because it initializes a grid with concrete values and without having a default for the given type it is not defined how those values will look like. So I don't really see a better approach here right now

becheran avatar Mar 27 '23 19:03 becheran