Home > Software design >  How do you deal with typedef and a Map?
How do you deal with typedef and a Map?

Time:11-15

For use in a package, I want to create a new set of typedef: IconAlignment.left IconAlignment.center IconAlignment.right

that will simply translate to: WrapAlignment.left WrapAlignment.center WrapAlignment.right

Im not clear how I can accomplish this with typedef. From what I could understand I can create a map to do this but I have been failing miserably:

typedef IconAlignment Map<String,WrapAlignment> = ['left': WrapAlignment.left, 'center': WrapAlignment.center, 'right':WrapAlignment.right];

CodePudding user response:

A typedef creates an alias to a type. WrapAlignment.left, WrapAlignment.center, and WrapAlignment.right are not types; they are values. However, WrapAlignment is a type:

typedef IconAlignment = WrapAlignment;

and now you can use IconAlignment.left, IconAlignment.center, and IconAlignment.right.

If you need to convert Strings to enum values, that's a separate problem. A typedef won't help you with that; it just creates type aliases.

  • Related