enums

Is it possible to manually define a conversion for an enum class?

北城以北 提交于 2020-01-31 04:04:48
问题 Normally you can define a cast for a class by using the following syntax: class Test { public: explicit operator bool() { return false; } }; Is there a way to do this or something similar for an enum class ? 回答1: No, it's not. Actually, an enum class is no class at all. The class keyword is only used because suddenly changing the unscoped enum to a scoped enum would have mean reworking all enums codes. So the committee decided that to distinguish between new-style and old-style enums, the new

PowerShell unable to use enum MultipleInstances

心不动则不痛 提交于 2020-01-30 11:38:06
问题 I am looking to use Powershell to set "if the task is already running, then the following rules applies: to "Do not start a new instance" my powershell script looks like below: $Task = Get-ScheduledTask -TaskName "MyJob" $Task.Settings.MultipleInstances= "StopExisting" Set-ScheduledTask -User "USER" -Password "PASS" $Task I get error as : Exception setting "MultipleInstances": "Cannot convert value "StopExisting" to type "Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask

Typescript enum values as array

佐手、 提交于 2020-01-30 04:59:13
问题 Is it possible to get the values of an enum in TypeScript as an array? Like this: enum MyEnum { FOO = 'foo', BAR = 'bar' } becomes ['foo', 'bar'] 回答1: Yes, it is possible to use: Object.values(MyEnum) because enum is an JS object after compilation: var MyEnum; (function (MyEnum) { MyEnum["FOO"] = "foo"; MyEnum["BAR"] = "bar"; })(MyEnum || (MyEnum = {})); 回答2: The simplest way to do it for a string enum is to use Object.values enum MyEnum { FOO = 'foo', BAR = 'bar' } console.log(Object.values

enum's rawValue property not recognized

送分小仙女□ 提交于 2020-01-30 04:12:17
问题 I'm using Xcode 6's playground to try out enums in Swift: enum Rank: String { case One = "One", Two="Two" init(rawValue : String) { self.rawValue = rawValue } } I want to override init so that the enum can be initialized using it's rawValue as argument. But I get an error: But according to the Apple's Swift guide my code should be correct. 回答1: Martin's answer is completely right. Here is a different view that more directly answers your question. In Xcode 6.0, an enum doesn't have a rawValue

How can we declare enumerations in QML, without any JavaScript?

拟墨画扇 提交于 2020-01-29 02:53:12
问题 Does QML allow us to define enums? If so, how we can declare enumerations in QML? I want to declare an enum in QML like the following C++ enum. If possible, I want to do this without any JavaScript. enum Color { RED, GREEN, BLUE }; Color r = RED; switch(r) { case RED : std::cout << "red\n"; break; case GREEN: std::cout << "green\n"; break; case BLUE : std::cout << "blue\n"; break; } What should I do? 回答1: You can use a pure Qml singleton, so you don't need any C++ or javascript. colors

How to use i18n with Rails 4 enums

懵懂的女人 提交于 2020-01-26 15:16:19
问题 Rails 4 Active Record Enums are great, but what is the right pattern for translating with i18n? 回答1: I didn't find any specific pattern either, so I simply added: en: user_status: active: Active pending: Pending... archived: Archived to an arbitrary .yml file. Then in my views: I18n.t :"user_status.#{user.status}" 回答2: Starting from Rails 5, all models will inherit from ApplicationRecord . class User < ApplicationRecord enum status: [:active, :pending, :archived] end I use this superclass to

#define inside an enum or how to extend an enum

江枫思渺然 提交于 2020-01-25 10:56:05
问题 I have several classes each of them uses the same enum but extends it a bit, based on its requirements. For example : class skirtColor{ enum Color{ red = 1, blue = 10, green = 12 }; }; class dressColor { enum Color{ red = 1, pink, yellow, blue = 10, green = 12 }; }; class pantsColor { enum Color { red = 1, brown, blue = 10, green = 12 }; }; Since there is no inheritance for enum in C++, I would like to use define for a common part #define COLOR\ // red color \ red = 1,\ // blue color \ blue =

How do I pass an enum in this context

故事扮演 提交于 2020-01-24 16:37:07
问题 I want to change the below function that I have written so that it will take the Enum (MyColors in this instance) as argument so that I can use the function on any suitable contiguous Enum. Everything I have tried so far has failed so I'm stuck. private enum MyColors { Red, Green, Blue } private String GetNext(String colorName) { MyColors colorValue; String colorNameOut = String.Empty; if (Enum.TryParse(colorName, out colorValue)) { MyColors initial = colorValue, next = colorValue; for (int i

Enum Class method with default Enum value fails

╄→尐↘猪︶ㄣ 提交于 2020-01-24 15:29:50
问题 I am well aware that if you have a class method that uses the enum's class name for type hinting, there is a hack to get it to work for Python 3.6 and below. Instead of... class Release(Enum): ... @classmethod def get(cls, release: Release): ... You need to use the string value like so... class Release(Enum): ... @classmethod def get(cls, release: "Release"): ... I believe in Python 3.7 and above there is a pythonic way around this "hack" where you don't have to use quotes. The reason is

Enum Class method with default Enum value fails

大城市里の小女人 提交于 2020-01-24 15:29:28
问题 I am well aware that if you have a class method that uses the enum's class name for type hinting, there is a hack to get it to work for Python 3.6 and below. Instead of... class Release(Enum): ... @classmethod def get(cls, release: Release): ... You need to use the string value like so... class Release(Enum): ... @classmethod def get(cls, release: "Release"): ... I believe in Python 3.7 and above there is a pythonic way around this "hack" where you don't have to use quotes. The reason is