Switch-case in haxe - haxe

I have a problem with my haxe code, So I have the following code :
var t : String = switch( id ) {
case 1 : Std.random( 2 ) == 0 ? Texts.list.0 : Texts.list.1;
case 2 : Std.random( 2 ) == 0 ? Texts.list.2 : Texts.list.3;
default: "";
}
For the default I get an error : This pattern is unused.
Can you help me please? Thx in advance

This is due that compiler detects patterns which will never match the input value http://haxe.org/manual/lf-pattern-matching-unused.html
In this case it might be bug https://github.com/HaxeFoundation/haxe/issues/4387
if You use haxe 3.2, try 3.1.3 to ensure.
In try.haxe your sample works well http://try.haxe.org/#9e54A

Related

Flutter | How to check if String does not contain a substring or a character

I'm learning Flutter/Dart and there is this check that i want to perform - to validate that a user who has entered an email should contain # in it
String myString = 'Dart';
// just like i can do the following check
myString.contains('a') ? print('validate') : print('does not validate')
// I want to do another check here
myString does not contain('#') ? print('does not validate'): print('validate')
Can someone suggest is there any inbuilt function to do such a thing.
Just simply put not ! operator (also known as bang operator on null-safety) on start
void main() {
String myString = 'Dart';
// just like i can do the following check
myString.contains('a') ? print('validate') : print('does not validate');
// I want to do another check here
!myString.contains('#') ? print('does not validate') : print('validate');
}

Parsing Terraform files with inner conditions

I'm trying to parse and configure a Terraform HCL configuration, using a script.
So far I've been using a tool named "hclq" (Link to github page).
Unfortunately, while this tool is great. When I have a conditional statement, such as:
resource "vault_identity_group" "group_a" {
count = terraform.workspace != "prod" ? 1 : 0
...
}
As documented by HashiCorp: Conditional Expressions
I tried encasing the condition as a literal:
count = ${terraform.workspace != "prod" ? 1 : 0}
But it seems that is no longer supported by Terraform.
Does anyone have any idea how can I get over this issue?
Thank you!
UPDATE: I've found an error in my literal, it should be encased in quotation marks:
count = "${terraform.workspace != var.prod ? 1 : 0}"
This way, the hclq tool can parse it as a string, I also had to switch the "prod" string with a variable, as the TF configuration does not support character escaping.

Ternary condition in El expression

When I use ternary condition in El Expression I get the eclipse warning message "cannot be result as a member" in the false expression.
#{sessionController.originalURI != null ?
sessionController.originalURI : request.contextPath}
In this case I got the message "contextPath cannot be resolved as a member of originalURI"
Try with this:
#{sessionController.originalURI ne null ?
sessionController.originalURI : request.contextPath}
I think it also could be an answer.
I don't know why but reversing the ternary solved the problem
#{sessionController.originalURI == null ? request.contextPath : sessionController.originalURI}
Use empty
http://docs.oracle.com/javaee/6/tutorial/doc/bnaik.html
#{not empty sessionController.originalURI ?
sessionController.originalURI : request.contextPath}
Valid too:
#{!empty sessionController.originalURI...

ANTLR4 DefaultErrorStrategy fails to inject missing token

I'm trying to run in TestRig the following grammar:
grammar COBOLfragment;
// hidden tokens
WS : [ ]+ -> channel(HIDDEN);
NL : '\n' -> channel(HIDDEN);
// keywords
PERIOD : '.';
DIVISION : 'DIVISION';
SECTION : 'SECTION';
DATA : 'DATA';
WORKING_STORAGE : 'WORKING-STORAGE';
FILE : 'FILE';
FD : 'FD';
EXTERNAL : 'EXTERNAL';
GLOBAL : 'GLOBAL';
BLOCK : 'BLOCK';
CONTAINS : 'CONTAINS';
CHARACTERS : 'CHARACTERS';
// data
INTEGER : [0-9]+;
ID : [A-Z][A-Z0-9]*;
dataDivision :
DATA DIVISION PERIOD
fileSection?
workingStorageSection?
;
fileSection :
FILE SECTION PERIOD
fileDescription*
;
fileDescription :
FD fileName=ID
// (IS? GLOBAL)? // 1. IS GLOBAL clause
// (IS? EXTERNAL)? // 2. IS EXTERNAL clause
blockClause?
PERIOD
;
blockClause :
BLOCK CONTAINS? blockSize=INTEGER CHARACTERS
;
workingStorageSection :
WORKING_STORAGE SECTION PERIOD
;
with the following input:
DATA DIVISION.
FILE SECTION.
FD FD01
WORKING-STORAGE SECTION.
Clearly the third line of input ("FD FD01") is missing the terminator PERIOD asked for in fileDescription rule.
The DefaultErrorStrategy correctly acknowledges this and conjures up the missing token:
On stderr the correct report is displayed: line 4:0 missing '.' at 'WORKING-STORAGE'.
But if the fragments commented out are enabled (that is, the clauses 'IS EXTERNAL' and 'IS GLOBAL' are brought in the grammar again), then single token insertion fails:
On stderr the misleading report is displayed: line 4:0 no viable alternative at input 'WORKING-STORAGE'
How to enable the full grammar (with IS EXTERNAL and IS GLOBAL clauses) retaining the ability to correct the missing PERIOD?
Side note 1: if I enable either IS EXTERNAL or IS GLOBAL, but not both clauses, then the DefaultErrorStrategy works nicely and injects the missing token.
Side note 2: the code generated for a grammar with both clauses enabled has the following extra code (compared to a grammar with just one of them enabled):
public final FileDescriptionContext fileDescription() ... {
...
try {
...
switch ( getInterpreter().adaptivePredict(_input,4,_ctx) ) {
case 1:
{
setState(31);
_la = _input.LA(1);
if (_la==IS) {
{
setState(30); match(IS);
}
}
setState(33); match(GLOBAL);
}
break;
}
...
}
catch (RecognitionException re) {
...
And the adaptivePredict() call is the culprit, because it throws no viable alternative at input 'WORKING-STORAGE' before the parser has a chance to match(PERIOD) (in the generated code, not pasted here).
I've managed to solve it adding a new clause for both IS clauses:
(here just the fragments changed)
...
fileDescription :
FD fileName=ID
isClauses?
blockClause?
PERIOD
;
isClauses :
IS? GLOBAL (IS? EXTERNAL)?
| IS? EXTERNAL
;
...
Now the DefaultErrorStrategy does its work and injects the missing PERIOD.
Why not isClauses : (IS? GLOBAL?) (IS? EXTERNAL)?;
Well, I tried that first, of course. But got a warning (warning(154): rule 'fileDescription' contains an optional block with at least one alternative that can match an empty string) and no missing PERIOD injected.

JSF: ExpressionLanguage if else expression

How can I do this? I want to show a value from a entity, not a string, but how can I do this as a valid code?
itemLabel="#{mandatoryFriendship.receiver == loginBean.currentMandatory ? mandatoryFriendship.sender.surname : mandatoryFriendship.receiver.surname mandatoryFriendship.receiver.name}"
Thank you for you help
Assuming at least EL 2.2 and that the names are non-null Strings you could have an expression of the form:
#{condition ? str0 : str1.concat(str2)}
For an older version (or null Strings) you could use:
#{condition ? str0 : str1}#{condition ? str0 : str2}

Resources