Switch with UInt16 value and case with int in swift 3 - switch-statement

I need to convert my project to swift 3.
I have a switch like this:
switch self.setupMan?.typeRegulation.value {
case 0: //
self.mainRegulationSetValue.text = String(Double(self.runtime.setPoint)/10)
break
case UInt16(1): //Giri motore
self.mainRegulationSetValue.text = String(self.runtime.setPoint)
break
case 2: //Portata
self.mainRegulationSetValue.text = String(Double(self.runtime.mch)/10)
break
default:
break
}
self.setupMan?.typeRegulation.value is a UInt16 var; Xcode get me this error:
Expression pattern of type 'Int' cannot match values of type 'Uint16?'
I tried to cast like this:
case UINt16(0):
but it doesn't work

Related

Dart Errors Unhandled

I'm new in Dart.
In the code bellow exception ccures :
void main(){
dynamic alpha = "String";
dynamic beta = 12;
print("Your code is so "+beta+" "+alpha);
}
Error :
type 'int' is not a subtype of type 'String'
Why when we use dynamic keyword to insist on telling the compiler for doing this job it's still got error? "combining string and other types"
When you declare your variable dynamic it does not mean the variable won't have a runtime type. It means the type can change:
dynamic alpha = 'String';
print(alpha.runtimeType); // prints String
alpha = 1;
print(alpha.runtimeType); // prints int
You can't do that with var. With var the compiler will infer the type, and it's fixed after that:
var beta = 'String';
print(beta.runtimeType);
beta = 1; // error: A value of type 'int' can't be assigned to a variable of type 'String'.
print(beta.runtimeType);
When you try to do "Your code is so " + beta you use the + operator of your String with an int paramter: beta.
You can see in the documentation that the + operator of String only accepts a String:
String operator +(String other);
If you wanted to use that operator you would have to convert the int variable to String:
print('Your code is so ' + beta.toString() + ' ' + alpha);
That's not remarkably beautiful. Instead of concatenation try string interpolation:
print('Your code is so $beta $alpha');

How to set a String into newly created Context's children

I have a parser that calls a visitX method with an XContext that contains an expression that resolves to an ArrayNode and a FunctionName. I can retrieve the function's object and want to call invoke on it for each element in this array but the function takes an ExpressionVisitor and a YContext. I can create a new YContext(XContext) and the children are empty as expected. I need to add my array.get(i) as a TerminalNode into the children array so the function receiving the YContext can check the number of children (1) and then get the value (e.g., ctx.exprValues().exprList().expr(0)) from the YContext.
TerminalNodeImpl can take a Token (which is an interface) and I haven't found a way to create a Token using the implementing classes that can take an JsonNode value (e.g., String, int, Object).
The YContext children is a List but I am not sure what implements ParseTree that I could construct using the JsonNode value.
I tried parsing the JsonNode value using code like this but I can't get anything in tokens that I could use addAnyChild to my new context...
for (int i=0;i<mapArray.size();i++) {
ANTLRInputStream input = new ANTLRInputStream(mapArray.get(i).asText());
MappingExpressionLexer lexer = new MappingExpressionLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
I am sure I'm overlooking something simple. In other situations I've been able to push the value onto the stack but in this case the functions I can call all take the YContext so I need to put the value into the YContext.children somehow.
The solution is convoluted but necessary based on how the expression is defined. I traced the ctx being passed to the function so I could get its structure:
Function_callContext:
---------------------
TerminalNodeImpl $string 44 '$string'
ExprValuesContext
TerminalNodeImpl ( 2 '('
ExpressionListContext
NumberContext
TerminalNodeImpl 1 22 '1'
TerminalNodeImpl ) 3 ')'
TerminalNodeImpl $string 44 '$string'
Because the parser is looking for:
expr :
...
| VAR_ID exprValues # function_call
...
;
...
exprList : expr (',' expr)* ;
exprValues : '(' exprList ')' ;
VAR_ID : '$' ID ;
ID : [a-zA-Z] [a-zA-Z0-9_]*;
and I found the CommonTokenFactory to let me create the Token I could put in the TerminalNodeImpl so I could build up the correct context.
Here is the code (I only implemented the NUMBER at this point but will add exceptions and other types later... My test was to transform an array of numbers to an array of strings using the $map([1..5],$string) example (where [1..5] is a sequence that becomes the array.
for (int i = 0; i < mapArray.size(); i++) {
Function_callContext callCtx = new Function_callContext(ctx);
// note: callCtx.children should be empty unless carrying an
// exception
ExprListContext elc = new ExprListContext(callCtx.getParent(),callCtx.invokingState);
ExprValuesContext evc = new ExprValuesContext(callCtx.getParent(),callCtx.invokingState);
evc.addAnyChild(new TerminalNodeImpl(CommonTokenFactory.DEFAULT.create(MappingExpressionParser.T__1,"(")));
CommonToken token = null;
JsonNode element = mapArray.get(i);
switch (element.getNodeType()) {
case ARRAY: {
break;
}
case BINARY:
break;
case BOOLEAN:
break;
case MISSING:
break;
case NULL:
break;
case NUMBER:
token = CommonTokenFactory.DEFAULT.create(MappingExpressionParser.NUMBER,element.asText());
TerminalNodeImpl tn = new TerminalNodeImpl(token);
NumberContext nc = new NumberContext(callCtx);
nc.addAnyChild(tn);
elc.addAnyChild(nc);
evc.addAnyChild(elc);
break;
case OBJECT:
break;
case POJO:
break;
case STRING:
break;
default:
break;
}
evc.addAnyChild(new TerminalNodeImpl(CommonTokenFactory.DEFAULT.create(MappingExpressionParser.T__1,")")));
callCtx.addAnyChild(var);
callCtx.addAnyChild(evc);
result = function.invoke(this, callCtx);
resultArray.add(result);
}

Use function to write down variable name in Arduino

I have a script in Arduino that'll get a letter, and to make things as short a possible I'd like to use a string combined with another variable to get the variable name I need to fill in into the function.
I'd like this because I have a function mySwitch.send(Int, 24). For the first Int variable I need to to send a number that is dependent on the letter I sent, and on the current value of A_stat, I defined these values in variables A_aan, A_uit, B_aan, B_uit, etc.
For example for the letter A I need to fill in the variable A_aan if a_stat == 0. If a_stat == 1 it needs to fill in A_off.
For B I need to fill in the variable B_aan if b_stat == 0, and I need to fill in the variable name B_uit on the place of Int if b_stat == 1.
Since the value of the variable vary at runtime, the only solution in my opinion is to use some functions.
For instance, if you want to get #_aan, where # is the value in a variable, you can use the following function:
int get_aan(char carat)
{
switch(carat)
{
case 'A':
return A_aan;
case 'B':
return B_aan;
...
}
return AN_INVALID_VALUE_YOU_DEFINE;
}
Remember to define an invalid value somewhere.
If you have to set that value, you can write a similar set function:
void set_aan(char carat, char value)
{
switch(carat)
{
case 'A':
A_aan = value;
break;
case 'B':
B_aan = value;
break;
...
}
}
You can also include some flags. For instance, if you wanted to get variable A_aan when A_stat = 0 and A_off when A_stat != 0, and repeat this for every variable, just modify the first function in:
int get_the_val(char carat)
{
switch(carat)
{
case 'A':
if (A_stat)
return A_off;
else
return A_aan;
case 'B':
if (B_stat)
return B_off;
else
return B_aan;
...
}
return AN_INVALID_VALUE_YOU_DEFINE;
}

How to specify character literal in groovy?

How do I specify a character literal in groovy since both 'a' and "a" result in string?
I do not want to declare a character variable just for this purpose.
Using the as keyword is the way to make a character literal in Groovy.
'a' as char
See the discussion here at Groovy's buglist.
If this is for a variable, you can also define the type, so:
import java.awt.image.*
new BufferedImage( 1, 1, BufferedImage.TYPE_INT_RGB ).with {
createGraphics().with {
// Declare the type
char aChar = 'a'
// Both ways are equivalent and work
assert fontMetrics.charWidth( aChar ) == fontMetrics.charWidth( 'a' as char )
dispose()
}
}
(apologies for the long example, but I had brain freeze, and couldn't think of a different standard java function that takes a char) ;-)
This also goes against the second line of the question, but I thought I'd add it for completeness
There a three ways to use char literals in Groovy:
char c = 'c' /* 1 */
'c' as char /* 2 */
(char) 'c' /* 3 */
println Character.getNumericValue(c) /* 1 */
println Character.getNumericValue('c' as char) /* 2 */
println Character.getNumericValue((char) 'c') /* 3 */
If you assign a String literal like 'c' to a variable, Groovy does the cast implicitly (see /* 1 * /). If you want use the String literals without variables, you have to cast them by using ...as char... (see /* 2 * /) or ...(char)... (see /* 3 * /).
The usage of char literals in methods without casting them is not possible as Groovy has only String literals which must be casted to char.
println Character.getNumericValue('c') // compile error
This response is rather late! But just stumbled upon it and wanted to add some clarification.
The more accurate Answer is unlike Java, Groovy does NOT have a character literal, but you can cast a string to a character. A literal is a value that is written exactly as it is to be interpreted, and the necessity of the type cast indicates it is NOT truly a literal.
Examples:
assert 'a'.class != Character.class
assert 'a'.class == String.class
assert ('a' as char).class == Character.class
assert ((char)'a').class == Character.class
char A = 'a'; // implicit coercion of string to char
assert A.class == Character.class
In contrast, both groovy and Java support numeric literals for int, long, double, and float, but do not support numeric literal for short.
Examples:
assert 42.class == Integer.class
assert 42l.class == Long.class
assert 42f.class == Float.class
assert 42d.class == Double.class
assert (42 as Short).class == Short.class

Can I jump from one case to another in a switch statements?

For switch statements, is it possible to change the value of the switch inside the switch statement so that it can jump around to different cases?
Ex:
int w = 0;
switch(w)
{
case 1:
doSomething();
w = 3;
case 2:
doSomething();
break;
case 3:
doSomething();
break;
}
Basically what I'm asking is, if I do not place a break statement for a case and I change the value of the switch in the same case, will the code execute both cases?
Yes you can change the value inside switch but it will not execute case for new value until you break in the case where you changed the value.
In your case it will not go in any case as there is no case for 0. But if you change to w = 1 then it will go for case 1 and then for case 2 as you do not have break; but it will not go for case 3.
No, it will not change and will not execute new case statement.
Remember that, once appropriate match is found with the case statement corresponding to a value inside the switch statement, that particular case is executed and once that is executed ( if break is provided after each case to prevent falling through all cases) , then the control returns to the end of switch statement.
Sample Code :
public class A {
public static void main(String [] args) {
int i=1;
switch(i) {
case 1 :
System.out.println("Case 1");
i = 2;
break;
case 2 :
System.out.println("Changed to Case 2");
break;
default:
System.out.println("Default");
break;
}
System.out.println("Final value of i " + i);
}
}
Output :
Case 1
Final value of i 2
Note : Inserting proper breakpoints, try to debug. You will come to know yourself, what exactly is happening.
If we do not give break after each case then Java will start executing the statement from matching case and keep on executing statements for following cases as well, until either a break statement is found or switch statements end is encountered.
If case 1 happens to execute, it's just a fall through to the case 2. And since there is a break in case 2, further fall through doesn't happen. It doesn't jump to case 3 because of the statement w = 3 ; in case 1.
No the switch's case will not change when changing the value in a particular case it checks only once
let a = 1
switch (a) {
case 1:
console.log("Hello from 1")//only this will be printed
a = 2//value of a is changed
break
case 2:
console.log("hello from 2")//this will not execute
a = 3
break
case 3:
console.log("hello from 3")//this will not execute
a = 4
break
case 4:
console.log("hello from 4")//this will not execute
break
default:
console.log("default")
}
console.log(a)//value will be 2

Resources