However, for many situations, your code will be simpler and easier to read by joining the if guard directly with the case statement. Below the categorization details, add up to three categorization. If (name = "Fred") println("Yubba dubba doo")Įlse if (name = "Bam Bam") println("Bam bam!") Submit a dispute of the website rating View the categorization as described above. Note that all of these examples could be written by putting the if tests on the right side of the expressions, like this: You can use this syntax whenever you want to add simple matches to your case statements on the left side of the expression. You can also extract fields from case classes and use those in your guards:Ĭase Person(name) if name = "Fred" => println("Yubba dubba doo")Ĭase Person(name) if name = "Bam Bam" => println("Bam bam!")Ĭase _ => println("Watch the Flintstones!") Imagine here that x is an instance of a Stock class that has symbol and price fields:Ĭase x if (x.symbol = "XYZ" & x.price buy(x)Ĭase x if (x.symbol = "XYZ" & x.price > 50) => sell(x) You can reference class fields in your if guards. Use it to match different values of an object:Ĭase x if x = 1 => println("one, a lonely number")Ĭase x if (x = 2 || x = 3) => println(x) Use it to match a range of numbers:Ĭase a if 0 to 9 contains a => println("0-9 range: " + a)Ĭase b if 10 to 19 contains b => println("10-19 range: " + b)Ĭase c if 20 to 29 contains c => println("20-29 range: " + c) SolutionĪdd an if guard to your case statement. You want to add qualifying logic to a case statement in a Scala match expression, such as allowing a range of numbers, or matching a pattern, but only if that pattern matches some additional criteria. Adguard Home Installed on Docker using PortainerAdGuard is the best way to get rid of annoying ads and online tracking and protect your computer from malware. This is Recipe 3.13, “How to add if expressions (guards) to match/case expressions.” Problem Thus, by configuring one application (AdGuard VPN), you can bypass the blocking in all other apps without fine-tuning additional settings Step 1. This is an excerpt from the 1st Edition of the Scala Cookbook (partially modified for the internet). Hello everybody, this is a small guide for Adguard Home, an equivalent alternative to Pi-Hole. show more info on classes/objects in repl.
0 Comments
Leave a Reply. |