|
|
|
|
|
by asm89
4779 days ago
|
|
It's hard to give a TL;DR on the whole library, but case classes can be used with for example pattern matching (https://github.com/lihaoyi/macropy#pattern-matching). The following code: with patterns:
Foo(x, Bar(3, z)) << Foo(4, Bar(3, 8))
The constructor after the << will be matched with the part before <<. It will only match if the first argument of "Bar" is 3. If that's the case, x and z will be bound to 4 and 8.Does this help? :) |
|
What's the purpose of only binding x and z if the first argument of Bar matches? What if it doesn't match? Is an exception thrown? Are x,z just None?
[Note: I'm talking about the example, not the @case decorator, which does seem useful]
Edit:
After reading the library examples, the explanation above is not entirely clear:
here's the rest of the example: I was thinking that somehow a new Foo instance was created only when the pattern matched. (Oh, and an exception is thrown when the match fails)