enums

What is an idiomatic way of representing enums in Go?

旧城冷巷雨未停 提交于 2021-02-17 05:49:31
问题 I'm trying to represent a simplified chromosome, which consists of N bases, each of which can only be one of {A, C, T, G} . I'd like to formalize the constraints with an enum, but I'm wondering what the most idiomatic way of emulating an enum is in Go. 回答1: Quoting from the language specs:Iota Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. It is reset to 0 whenever the reserved word const appears in the source and increments

Simulating field inheritence with composition

陌路散爱 提交于 2021-02-17 05:04:34
问题 I have several pairs of structs for which the fields of one is a perfect superset of the other. I'd like to simulate some kind of inheritance so I don't have to have separate cases for each struct since that would almost double my code. In a language like C, I could simulate inheritance of fields with something like this: struct A { int a; }; struct B { struct A parent; int b; }; main() { struct B test1; struct A *test2 = &test1; test2->a = 7; } I want to do something like this in Rust. I

Why is it not possible to use “Using static” feature with private enum? Is there any alternative?

一个人想着一个人 提交于 2021-02-16 20:32:27
问题 I have this class where I use a private enum. I would like to use C# 6 "Using static" feature, like the following: using static ConsoleForSimpleTests.Foo.MyEnum; namespace ConsoleForSimpleTests { public class Foo { private enum MyEnum { I, DonT, Want, This, To, Be, Public } private MyEnum value; public void SomeMethod() { switch (value) { case I: case DonT: case Want: case This: case To: case Be: case Public: break; } } } } NOTE: This does not compile and I understand why, it is due to the

Enum helper in c# not giving expected result

℡╲_俬逩灬. 提交于 2021-02-16 20:18:22
问题 Basically I am not recieving the correct enum type for some reason and I cannot figure out why, my code is below, many thanks in advance for any pointers/ explanation... EDIT: type-> changed to anothername (thanks guys for the heads up) Helper: public static T Convert<T>(this string str) { return (T)Enum.Parse(typeof(T), str, true); } Enum values: public enum anothername { SmallText = 100, Number = 15, TextArea = 0, Bool = 0, Choices = 0, } My test: [Test] public void

Importing with package name breaking enum comparison in Python

被刻印的时光 ゝ 提交于 2021-02-16 18:58:31
问题 My friend and I are making chess AIs in Python, but we're running into a mysterious problem with enums. We encode the piece types in an enum like so: Piece.py: from enum import Enum class PieceType(Enum): type_one = 1 ... def recognise_type(my_type): print("Passed ", my_type) if my_type is PieceType.type_one: print("Type One") else: print("Type not recognised") We ask the AI for a piece (for promoting a pawn for instance) and call recognise_type: ai.py: import Piece def get_promotion():

How to make a Swift enum with associated values equatable

泄露秘密 提交于 2021-02-15 09:12:27
问题 I have an enum of associated values which I would like to make equatable for testing purposes, but do not know how this pattern would work with an enum case with more than one argument. For example, summarised below I know the syntax for making heading equatable. How would this work for options, which contains multiple values of different types? enum ViewModel { case heading(String) case options(id: String, title: String, enabled: Bool) } func ==(lhs: ViewModel, rhs: ViewModel) -> Bool {

How to make a Swift enum with associated values equatable

浪尽此生 提交于 2021-02-15 09:11:11
问题 I have an enum of associated values which I would like to make equatable for testing purposes, but do not know how this pattern would work with an enum case with more than one argument. For example, summarised below I know the syntax for making heading equatable. How would this work for options, which contains multiple values of different types? enum ViewModel { case heading(String) case options(id: String, title: String, enabled: Bool) } func ==(lhs: ViewModel, rhs: ViewModel) -> Bool {

Foreach on enum types in template

和自甴很熟 提交于 2021-02-11 13:03:53
问题 enum MyEnum { type1, type2, type3 } public void MyMethod<T>() { ... } How to make forach on enum to fire MyMethod<T> on every enum? I try something with foreach (MyEnum type in Enum.GetValues(typeof(MyEnum))) {...} But still don't know how to use this type inside foreach with MyMethod<T> as T 回答1: Is this what you are trying to do? class Program { static void Main(string[] args) { EnumForEach<MyEnum>(MyMethod); } public static void EnumForEach<T>(Action<T> action) { if(!typeof(T).IsEnum)

Create form for domain object with multiselect Enum field fails with 'Property xxx is type-mismatched'

别来无恙 提交于 2021-02-11 13:00:42
问题 I am using Grails 4.0.4 w/ GORM 7.0.7.RELEASE The database I am using is MongoDB I can successfully run the application and navigate to the create form for the domain class. I can select the value for the singleton field someOtherEnum . I can multiselect values for the categories field. When I submit the form, however, I get this message: 'Property categories is type-mismatched'. I used this approach based on the answers to another question posted here, but it's not working for me. I am not

Python: How to decode enum type from json

对着背影说爱祢 提交于 2021-02-11 06:48:44
问题 class MSG_TYPE(IntEnum): REQUEST = 0 GRANT = 1 RELEASE = 2 FAIL = 3 INQUIRE = 4 YIELD = 5 def __json__(self): return str(self) class MessageEncoder(JSONEncoder): def default(self, obj): return obj.__json__() class Message(object): def __init__(self, msg_type, src, dest, data): self.msg_type = msg_type self.src = src self.dest = dest self.data = data def __json__(self): return dict (\ msg_type=self.msg_type, \ src=self.src, \ dest=self.dest, \ data=self.data,\ ) def ToJSON(self): return json