how to get enum by index and vice versa - flutter

I just looked for the below questions and did not find a complete answer. So this is what I found out
how can I get an enum value by index
how can I get the index position of an enum value

for the following enum
enum Status {
none,
running,
stopped,
paused
}
Status.values[1] provides Status.running
Status.running.index provides 1
if you have a variable Status myStatus; you can substitute Status.running with myStatus

Related

Swifty way of naming a boolean property

1.
According to the swift API design guidelines, a boolean property should read as assertions

> Uses of Boolean methods and properties should read as assertions about
the receiver when the use is nonmutating,
e.g. x.isEmpty, line1.intersects(line2).
2.
I would like to make a computed property of which type is Boolean to the existing data type.
Here is a simplified version of my code:
struct State {
var authorID: String
var myID: String
var `XXX`: Bool {
return myID == authorID
}
}
I want the property XXX to stand for whether I am author or not.
I firstly came up with the names like authorIsMe, iAmAuthor, isAuthorMe, etc. but realized that it didn’t read as assertions about the receiver.
So, what name do you think fit best for XXX? Any idea will be appreciated.
Thank you
(Please do not consider inlining the expression myID == authorID because in the original code, it is not short as above so I need the computed property)
amITheAuthor is the best property name according to me as it will clearly throw the answer & its means of use , its a suggestion you can use this as well.

Doxygen enum with explicit hard value

I have a C enum with around 50 values (they are error codes). Besides the first value, the numbering is kept automatic.
typedef enum {
MY_STATUS_OK = 0,
MY_STATUS_ERROR_UNKNOWN,
MY_STATUS_ERROR_A,
MY_STATUS_ERROR_B,
/* ... */
MY_STATUS_ERROR_LAST
} my_status_t;
I do get an enum description in the HTML, but it doesn't show the explicit value, only the enum values' name.
I want to document the association between hard value and enum value name. Right now when I get an error value, I need to manually count the index in the enum to find out what it means.

Use of Unresolved Identifier for Function // Swift

UPDATE BELOW
I am currently in an introductory Swift course at my college and am struggling with functions. I thought I followed the instructions clearly, but I am getting a certain error about "use of an unresolved identifier."
This is the full error:
error: My Functions Playground 2 2.playground:23:8: error: use of unresolved identifier 'newGPA'
switch newGPA {
Here is my code (the original instructions are below):
var gpa: Int
var attemptedHours: Int
var earnedGradePoints: Int
// create your function here
func gpaUpdater(hours moreHours: Int, grade moreGPA: Int) {
let _: Int = attemptedHours + moreHours
let newGPA: Int = gpa + moreGPA
print(newGPA)
}
// call the function
gpaUpdater(hours: 16, grade: 60)
// add the new hours and grade points here and call the function again
switch newGPA {
case 0...1.8:
print("You will be placed on suspension")
case 1.8...2.0:
print("You will be placed on probation")
case 3.5...3.8:
print("You will be put on the dean's list.")
case 3.9:
print("You will be put on the president's list.")
default:
print("Carry on. Nothing to see here.")
}
Instructions:
We're going to track your GPA from one semester to the next. Assume at the end of your sophomore years, you have attempted 60 hours and have earned 222.5 grade points. Assign attempted hours and grade points to variables. Write a function that updates your current GPA and assigns it to the GPA var (you'll update it along the way). Label your function arguments. Print your new GPA from within the function.
At the end of the current semester, add 16 hours and 60 grade points to your record. Call the gpa function to update your overall gpa.
Test your gpa at the end of the year to see if any administrative action needs to be taken. If the gpa is less than 1.8, the student will need to be placed on suspension. If less than 2.0, we need to put the student on probation. If over 3.5, we'll put the student on the dean's list, and if over 3.9, we'll put the student on the president's list. Create a switch that prints the recommended adminstrative action. If no action is required, print, "Carry on. Nothing to see here." Create internal and external labels for your arguments.
Thank You for your help!
UPDATE
The function part of my Swift code is now correct, thank you all for the help. Now I am trying to fix my switch statement. Here is my code:
// add the new hours and grade points here and call the function again
switch gpa {
case gpa > 1.8:
print("You will be placed on suspension")
case 1.8...2.0:
print("You will be placed on probation")
case 3.5...3.8:
print("You will be put on the dean's list.")
case gpa > 3.9:
print("You will be put on the president's list.")
default:
print("Carry on. Nothing to see here.")
}
The problem, I think, is that my teacher wants GPA to be an int, but if I want to use values like 1.9 for the gpa, then it needs to be a double. Here is an error that I am getting:
error: My Functions Playground 2 2.playground:26:10: error: binary operator '>' cannot be applied to operands of type 'Int' and 'Double'
case gpa > 1.8
Scope. Scope. Scope.
newGPA is declared locally in the scope of gpaUpdater. It's not visible on the top level.
You could do
// create your function here
func gpaUpdater(hours moreHours: Int, grade moreGPA: Int) -> Int {
// let _: Int = attemptedHours + moreHours
return gpa + moreGPA
}
// call the function
let newGPA = gpaUpdater(hours: 16, grade: 60)
// add the new hours and grade points here and call the function again
switch newGPA { ...
No comment about the (unused) first parameter of gpaUpdater and the floating point cases switching over an Int 😉
I'm going to answer this from an assignment perspective; the other answers regarding returning the local variable value are correct for accessing your newGPA variable.
You missed the point in the assignment by creating the "newGPA" variable. The assignment states to "update" the global gpa variable with the new value from within the function.
If this is introductory coding, you may not have come across the concept of recursion. This is basically assigning some value by including itself in the calculation.
Instead of
let newGPA: Int = gpa + moreGPA
print(newGPA)
think
gpa = gpa + moreGPA
print(gpa)
which can also be written as
gpa += moreGPA
and then use gpa in your switch function.
What this does is updates your global gpa variable to a new value (by adding the moreGPA to it). This is one of the main strengths of a global variable. It can be accessed and modified from anywhere in your program.
That's my understanding based on the assignment instructions. That said, returning a value from a function is cleaner (in my opinion) since global variables can become conflicts in more complicated programs.

Type wrapping of non-atomic types in golang

I'm new to golang and am trying to understand a code example of type wrapping for the "non-atomic" type time.Time.
The type extension in question is from the Go client for GDAX on github, go-coinbase-exchange project.
The expected behavior would be for Time variables from the project (coinbase.Time), which are of type Time time.Time (as defined in the project's time.go file) to behave something like the following example for extending the "atomic" type int (from blog.riff.org in that they might follow a kind of "inheritance" from the base type for functions like Time.format (from golang's standard implementation of time:
package main
import "fmt"
type Int int
func (i Int) Add(j Int) Int {
return i + j
}
func main() {
i := Int(5)
j := Int(6)
fmt.Println(i.Add(j))
fmt.Println(i.Add(j) + 12)
}
But if I modify the code example from the project's List Account Ledger example found in Readme.md to include a print function which might otherwise give me a human-readable view of the CreatedAt struct variables (as follows), I get a compiler error saying that "type coinbase.Time has no field or method Format":
for _, e := range ledger {
print("Entry Creation: ")
fmt.Printf(e.CreatedAt.Format("2006-01-02 15:04:05.999999+00"))
}
The expected behavior inside the for loop would be for it to print the ledger entries in a human-readable format. I can get the contents of the structs, but I'm not really sure how to then use the resulting wall, ext and loc members.
For example, inserting fmt.Printf("%#v", e.CreatedAt) into the for loop gives me a representation of the time that looks something like this:
coinbase.Time{wall:0x3015a123, ext:63612345678, loc:(*time.Location)(nil)}
{806986000 63638738354 <nil>}
I can also see that wall is of type uint64, that ext is of type int64 and that loc is just GMT/UTC=0 by formatting the variable as a string because fmt.Printf("%s", e.CreatedAt) gives me output which is similar to the following:
{%!s(uint64=712345678) %!s(int64=63612345678) %!s(*time.Location=<nil>)}
It seems like I'm missing something. I've requested further information through issues tab on github, but maybe this is a nube question. So I'm not sure how quick the response time would be, and I'm interested in the more general case for extending non-atomic types in go.
Named types do not inherit any methods from the underlying type (indeed there is no inheritance at all in Go); you must cast to the underlying type to call any methods from that type:
(time.Time(e.CreatedAt)).Format("2006-01-02 15:04:05.999999+00")

Group-documenting a group of enum mebers

Is it possible to document a group of enum values, similar to this (this example doesn't work):
enum MyEnum
{
MYENUM_FIRST,
//#{
//#name These two members' description
MYENUM_SECOND,
MYENUM_THIRD,
//#}
MYENUM_FOURTH, ///third member documentation
MYENUM_FIFTH
};
Two immediate things which stand out:
You are not documenting MYENUM_SECOND or MYENUM_THIRD since you have not wrapped them with valid doxygen comments: your should write ///#{ instead of //#{.
Your documentation of MYENUM_FOURTH should be
///< third member documentation
otherwise you are documenting MYENUM_FIFTH instead of MYENUM_FOURTH.
I could get documentation for both MYENUM_SECOND and MYENUM_THIRD by using the following code
enum MyEnum
{
MYENUM_FIRST,
///#{
/// These two members' description
MYENUM_SECOND,
MYENUM_THIRD,
///#}
MYENUM_FOURTH, ///< third member documentation
MYENUM_FIFTH
};
and by setting DISTRIBUTE_GROUP_DOC = YES in the configuration file. Note that I have removed the \name command from your example.