Jump to content
Sign in to follow this  
Xpect92

Wildcards in switch statement with tuples or workaround

Recommended Posts

Hello Guys,

 

modern programming languages often have something like wildcards and tuples that i can use in for example switch statements. E.g. in Swift:

 

let string = "String"
let int = 123

switch (string, int) {
	case ("String", 123):
  		print("Perfect match.")
  	case ("String", _):
  		print("String matches - doesnt matter what int says")
  	default:
  		print("You get the idea.")
}

 

As far as i know neither exists in SQF, right?

 

I was thinking about getting something similar done because i have certain if chains that seem unmaintainable in the future. Can i maybe accomplish this with arrays? Like:

 

_var = ["String", 123];

switch (_var) do {
	case ["String", 123]: {
    	diag_log("Perfect match.");
	}
    case ["String", /*Any idea how i can ignore the second value?*/]: {
    	diag_log("String matches - ignore int");
    }
    default: {
    	
    }
}

 

I just dont see how i can possibly ignore a value in the comparison. Any ideas how to accomplish that or do you think its not possible at all?

 

Best Regards

Share this post


Link to post
Share on other sites

Switch may provide some performance improvement over if...else and make the code prettier but unless you're running this statement on every frame, it should not really be noticeable.

 

If you need some more complicated logic, just do it separately, imo. What do you mean by unmaintainable? You could use "in" or even use it in a for loop to create a more complicated condition.

  • Like 1

Share this post


Link to post
Share on other sites
9 hours ago, Xpect said:

As far as i know neither exists in SQF, right?

 

Not exactly, but you can just do a switch(true) and have your cases evaluate to boolean, which should be nice enough anyways:

switch (true) do
{
	case ((_var select 0) isEqualTo "String"):
	{
		if ((_var select 1) isEqualTo 123) then
		{	
			diag_log text "match both";
		} else
		{
			diag_log text "match string";
		};
	};

	case ((_var select 1) isEqualTo 123):
	{
		diag_log text "match number";
	};

	default
	{
		diag_log text "no match";
	};
};

 

  • Like 4

Share this post


Link to post
Share on other sites

You can also use BIS_fnc_bitflagsCheck for more complex conditions. Comes with all sorta functions to enhance functionality.

Another possibility would be to use arrayIntersect to check for common elements, returns the elements that are common to both arrays or an empty array.

 

Cheers

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks for the answers.

 

14 hours ago, theend3r said:

Switch may provide some performance improvement over if...else and make the code prettier but unless you're running this statement on every frame, it should not really be noticeable.

 

If you need some more complicated logic, just do it separately, imo. What do you mean by unmaintainable? You could use "in" or even use it in a for loop to create a more complicated condition.

 

I am not really worried about performance. Just readability and getting the ability to easily understand the code in lets say a year after not touching it.

 

9 hours ago, rübe said:

 

Not exactly, but you can just do a switch(true) and have your cases evaluate to boolean, which should be nice enough anyways:


switch (true) do
{
	case ((_var select 0) isEqualTo "String"):
	{
		if ((_var select 1) isEqualTo 123) then
		{	
			diag_log text "match both";
		} else
		{
			diag_log text "match string";
		};
	};

	case ((_var select 1) isEqualTo 123):
	{
		diag_log text "match number";
	};

	default
	{
		diag_log text "no match";
	};
};

 

 

I like the idea but that still leaves me with a lot of manual comparing of values. I thought about my problem again and figured that my conditions might be too complex to handle in a simple switch after all. Im just so despratly trying to get rid of those ugly nested if statements.

 

8 hours ago, Grumpy Old Man said:

You can also use BIS_fnc_bitflagsCheck for more complex conditions. Comes with all sorta functions to enhance functionality.

Another possibility would be to use arrayIntersect to check for common elements, returns the elements that are common to both arrays or an empty array.

 

Cheers

 

I'll check those out tonight BIS_fnc_bitflagsCheck looks very interesting. I'll have to check how much of an overhead it creates though.

 

Thanks!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×