digitalmars.D.learn - several types in one Array ?
- Mil58 (6/6) Oct 17 2019 Hi all...
- Paul Backus (4/10) Oct 17 2019 import std.variant;
- Mil58 (2/13) Oct 17 2019 T h a n ks for this(quick) reply :)
- Ron Tarrant (94/97) Oct 17 2019 You actually have at least three options for handling mixed data:
Hi all... Does exist (or not?) a way to create array with several types (as in Python) (example) : xxx[4] myTab = [180, "John", false, -0.771]; Of course, this is doing an Error ;-) Thanks.
Oct 17 2019
On Thursday, 17 October 2019 at 14:45:24 UTC, Mil58 wrote:Hi all... Does exist (or not?) a way to create array with several types (as in Python) (example) : xxx[4] myTab = [180, "John", false, -0.771]; Of course, this is doing an Error ;-) Thanks.import std.variant; Variant[4] myTab = variantArray(180, "John", false, -0.771); Documentation: https://dlang.org/phobos/std_variant.html
Oct 17 2019
On Thursday, 17 October 2019 at 14:50:23 UTC, Paul Backus wrote:On Thursday, 17 October 2019 at 14:45:24 UTC, Mil58 wrote:T h a n ks for this(quick) reply :)Hi all... Does exist (or not?) a way to create array with several types (as in Python) (example) : xxx[4] myTab = [180, "John", false, -0.771]; Of course, this is doing an Error ;-) Thanks.import std.variant; Variant[4] myTab = variantArray(180, "John", false, -0.771); Documentation: https://dlang.org/phobos/std_variant.html
Oct 17 2019
On Thursday, 17 October 2019 at 14:45:24 UTC, Mil58 wrote:Hi all... Does exist (or not?) a way to create array with several types (as in Python)You actually have at least three options for handling mixed data: ``` // Three ways to handle mixed data import std.stdio; import std.typecons; import std.variant; struct Result { string itemName; int quantity; bool needIt; } // struct Result class GroceryItem { private: bool _needIt; int _quantity; string _itemName; public: this(string itemName, int quantity, bool needIt) { _itemName = itemName; _quantity = quantity; _needIt = needIt; } // this() Variant getVariant() { return(cast(Variant)tuple(_itemName, _quantity, _needIt)); // use with a Variant return type } // getVariant() Tuple!(string, int, bool)getTuple() { return(tuple(_itemName, _quantity, _needIt)); // use with a Tuple!(string, int, bool) return type } // getTuple() Result getStruct() { Result result = {_itemName, _quantity, _needIt}; return(result); } // getStruct() void set(bool needIt) { _needIt = needIt; } // set() } // class GroceryItem void main(string[] args) { GroceryItem[] groceryList; GroceryItem item1 = new GroceryItem("Paper Towels", 1, true); groceryList ~= item1; GroceryItem item2 = new GroceryItem("Bread", 2, true); groceryList ~= item2; GroceryItem item3 = new GroceryItem("Butter", 1, false); groceryList ~= item3; GroceryItem item4 = new GroceryItem("Milk", 1, true); groceryList ~= item4; GroceryItem item5 = new GroceryItem("Potato Chips", 3, false); groceryList ~= item5; GroceryItem item6 = new GroceryItem("Pickles", 4, true); groceryList ~= item6; writeln("Returning variants..."); foreach(groceryItem; groceryList) { writeln(groceryItem.getVariant()); } writeln(); writeln("Returning tuples..."); foreach(groceryItem; groceryList) { writeln(groceryItem.getTuple()); } writeln(); writeln("Returning structs..."); foreach(groceryItem; groceryList) { writeln(groceryItem.getStruct()); } } // main() ```
Oct 17 2019