Scala - implicit conversion with unapply?

Implicit conversions are not applied when pattern matching. That's not a bug or a problem with your code, it's simply a design decision of the creators of Scala To fix it, you should write another extractor that accepts a String — which in turn can call your implicit conversion Alternatively, you can try with a view bound, which seems to work as well, and will also work if you later define other implicit conversions to MyString : object Apply { def unapplyS.

Implicit conversions are not applied when pattern matching. That's not a bug or a problem with your code, it's simply a design decision of the creators of Scala. To fix it, you should write another extractor that accepts a String — which in turn can call your implicit conversion.

Alternatively, you can try with a view bound, which seems to work as well, and will also work if you later define other implicit conversions to MyString: object Apply { def unapplyS.

Thanks. That's a bit disappointing. Do you know what the motivation is for that decision?

– dhg Jul 18 at 17:19 Yep, adding def unapply(p: String): OptionString = Some(p) to Apply does the trick. So I'll go with that. Thanks.

– dhg Jul 18 at 17:22 @dhg I edited the answer — a view bound seems to work as well. – Jean-Philippe Pellet Jul 18 at 18:36 Awesome! The view bound is a great solution.

Not only does it not require writing additional methods for every possible type, but it solves another problem I had: If unapply takes a tuple, you can't use the overloading trick due to type erasure. But this solves it cleanly and results in compile-time errors for wrong arg types: def unapplyS S + p. _2).

– dhg Jul 18 at 19:13.

I'd like an extractor to implicitly convert its parameters, but it doesn't seem to work. Can anyone explain why it fails to convert the parameter from String to MyString? I would expect it to call string2mystring("a") on the fly.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions