pattern-matching

How do I create a Rust macro with optional parameters using repetitions?

帅比萌擦擦* 提交于 2020-05-26 11:22:58
问题 I'm currently looking into Rust macros and I can not find any detailed documentation on repetitions. I would like to create macros with optional parameters. This would be my idea: macro_rules! single_opt { ($mand_1, $mand_2, $($opt:expr)* ) =>{ match $opt { Some(x) => println!("1. {} 2. {}, 3. {}", $mand_1, $mand_2, x); None => single_opt!($mand_1, $mand_2, "Default"); } } } fn main() { single_opt!(4,4); } This example seems to be outdated, since I can not compile it. The Rust book mentions

How do I create a Rust macro with optional parameters using repetitions?

丶灬走出姿态 提交于 2020-05-26 11:22:20
问题 I'm currently looking into Rust macros and I can not find any detailed documentation on repetitions. I would like to create macros with optional parameters. This would be my idea: macro_rules! single_opt { ($mand_1, $mand_2, $($opt:expr)* ) =>{ match $opt { Some(x) => println!("1. {} 2. {}, 3. {}", $mand_1, $mand_2, x); None => single_opt!($mand_1, $mand_2, "Default"); } } } fn main() { single_opt!(4,4); } This example seems to be outdated, since I can not compile it. The Rust book mentions

Why can't I return a concrete subtype of A if a generic subtype of A is declared as return parameter?

和自甴很熟 提交于 2020-05-24 06:08:11
问题 abstract class IntTree object Empty extends IntTree case class NonEmpty(elem: Int, left: IntTree, right: IntTree) extends IntTree def assertNonNegative[S <: IntTree](t: S): S = { t match { case Empty => Empty // type mismatch, required: S, found: Empty.type case NonEmpty(elem, left, right) => if (elem < 0) throw new Exception else NonEmpty(elem, assertNonNegatve(left), assertNonNegative(right)) // req: S, fd: NonEmpty.type } } This is my failed attempt of implementing the function with

Pattern matching instanceof [closed]

混江龙づ霸主 提交于 2020-05-23 06:50:27
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 hours ago . I came across this amazing topic on https://www.baeldung.com/java-pattern-matching-instanceof. But when I try to run the following code, it throws compile time error: if(obj instanceof String s) { System.out.println(s); } I am using Java 14. 回答1: This is a preview feature in Java 14, see JEP 305 and

Pattern matching instanceof [closed]

半腔热情 提交于 2020-05-23 06:50:07
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 hours ago . I came across this amazing topic on https://www.baeldung.com/java-pattern-matching-instanceof. But when I try to run the following code, it throws compile time error: if(obj instanceof String s) { System.out.println(s); } I am using Java 14. 回答1: This is a preview feature in Java 14, see JEP 305 and

Convert type pattern maching to type class

无人久伴 提交于 2020-05-15 02:28:26
问题 I have the following code: val byteBuffer = array(0) match { case _: Int => ByteBuffer.allocate(4 * array.length) case _: Long => ByteBuffer.allocate(8 * array.length) case _: Float => ByteBuffer.allocate(4 * array.length) case _: Double => ByteBuffer.allocate(8 * array.length) case _: Boolean => ByteBuffer.allocate(1 * array.length) } How can I convert it to use type class? Edit: I was asked what the type of the array is. It's complicated. The array is declared like that: val array = obj

How to write scala matcher for class?

时光怂恿深爱的人放手 提交于 2020-05-13 08:00:26
问题 Assume I have follwing code def get[T](name:String)(implicit mf:ClassManifest[T]):T = mf.erasure match { case classOf[Boolean] => obj.getBoolean(name) case classOf[Int] => obj.getInt(name) } Now code dosn't work because classOf[Int] is invalid match value. 回答1: You should almost certainly investigate alternatives to using manifests and matching on class objects. In this case type classes will provide a much cleaner solution, // Assuming that Store is the type of obj ... trait Get[T] { def get

Pattern matching on Class[_] type?

不羁岁月 提交于 2020-05-08 20:48:11
问题 I'm trying to use Scala pattern matching on Java Class[_] (in context of using Java reflection from Scala) but I'm getting some unexpected error. The following gives "unreachable code" on the line with case jLong def foo[T](paramType: Class[_]): Unit = { val jInteger = classOf[java.lang.Integer] val jLong = classOf[java.lang.Long] paramType match { case jInteger => println("int") case jLong => println("long") } } Any ideas why this is happening ? 回答1: The code works as expected if you change

Pattern matching on Class[_] type?

五迷三道 提交于 2020-05-08 20:47:11
问题 I'm trying to use Scala pattern matching on Java Class[_] (in context of using Java reflection from Scala) but I'm getting some unexpected error. The following gives "unreachable code" on the line with case jLong def foo[T](paramType: Class[_]): Unit = { val jInteger = classOf[java.lang.Integer] val jLong = classOf[java.lang.Long] paramType match { case jInteger => println("int") case jLong => println("long") } } Any ideas why this is happening ? 回答1: The code works as expected if you change

Does Haskell provide an idiom for pattern matching against many possible data constructors?

余生颓废 提交于 2020-05-08 14:28:05
问题 Working on a Haskell project, I'm dealing with the Event data type from the FSNotify package. The constructors for Event are all: Added FilePath UTCTime Modified FilePath UTCTime Removed FilePath UTCTime In my code, I'm only interested in extracting the FilePath from the Event and doing the same action regardless of the type constructor; because of this, I'm tempted to make a lambda. Unfortunately, the code suffers reduced readability when I drop a case expression into the lambda to pattern