Compiler error does not help me figure out the problem with this code: #155
-
Each time I try to replace my current parsers with swift-parsing I get thwarted by compiler errors almost immediately. It's not at all obvious to me how to fix them though. Here is an example of what I'm struggling with. No matter what I try, I cannot get it to compile so I must be missing something fundamental.
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I'm going to convert this to a discussion and answer over there. |
Beta Was this translation helpful? Give feedback.
-
There's a few things going on here that are causing bad error messaging. First of all, let stackElement = Parse {
StackElement.number($0)
} with: {
- Int.parser().map { Double($0)! }
+ Int.parser().map { Double($0) }
} Now the error message is "Ambiguous use of 'init(_:)'". This is happening because let stackElement = Parse {
StackElement.number($0)
} with: {
- Int.parser().map { Double($0) }
+ Int.parser(of: Substring.self).map { Double($0) }
} And now it compiles. It's worth mentioning that you will only run into this ambiguity problem when using try Parse {
Int.parser()
"!"
}
.parse("42!") // 42 Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Thanks. I was trying everything including the errant “!” to get a successful compile. But this is a super helpful explanation.
My existing parsers use a Parseable protocol for the inputs so I have a ParseableString type which uses Substring. I think this is why I struggle to convert my parsers so your suggestion should put that right.
…Sent from my iPhone
On 27 Feb 2022, at 17:13, Brandon Williams ***@***.***> wrote:
There's a few things going on here that are causing bad error messaging. First of all, Double's initializer is not failable so you can drop the force unwrap:
let stackElement = Parse {
StackElement.number($0)
} with: {
- Int.parser().map { Double($0)! }
+ Int.parser().map { Double($0) }
}
Now the error message is "Ambiguous use of 'init(_:)'". This is happening because Int.parser() is overloaded quite a bit in order to support parsing on multiple string representations such as Substring and UTF8View, and in the current situation it can't figure out the type of input. You can explicitly specify the input to fix it:
let stackElement = Parse {
StackElement.number($0)
} with: {
- Int.parser().map { Double($0) }
+ Int.parser(of: Substring.self).map { Double($0) }
}
And now it compiles.
It's worth mentioning that you will only run into this ambiguity problem when using Int.parser() in isolation, which from our experience is rare. Usually it's used in conjunction with other parsers that help Swift figure out the types, e.g.:
try Parse {
Int.parser()
"!"
}
.parse("42!") // 42
Hope this helps!
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you authored the thread.
|
Beta Was this translation helpful? Give feedback.
There's a few things going on here that are causing bad error messaging. First of all,
Double
's initializer is not failable so you can drop the force unwrap:Now the error message is "Ambiguous use of 'init(_:)'". This is happening because
Int.parser()
is overloaded quite a bit in order to support parsing on multiple string representations such asSubstring
andUTF8View
, and in the current situation it can't figure out the type of input. You can explicitly specify the input to fix it:let stackElement = Parse { StackElement.number($0) } with: { - …