A bit of context: I am in the process of writing a micro-optimized parser for some ASCII data (a lot of it). Parse functions can return a value, or error information (“parsing failed at character …, for reasons …”). So it is like a Nullable
, but the “null” case has more information than a Boolean
(an Int
does fine in practice).
I first implemented it like a Nullable
with extra info,
struct MaybeParsed{S,T}
info::S
value::T
MaybeParsed{T}(info::S) where {T,S} = new{T,S}(info)
MaybeParsed(info::S, value::T) where {T,S} = new{T,S}(info, value)
end
When combining these parsers (eg parse multiple fields in a line) I ran into the predictable nightmare of calculating the return type when there is no value.
Then I switched to the following: for errors, return a ParserError(::S)
object, otherwise the value. S
is a bits type (again, Int
for most of the time). In 0.6
, the speed is only nominally affected (about 5%). In master
, it is faster.
So I am happy, but would like to design my code in a way that it would be performant in 0.7
. I am of course OK with sacrificing a bit of speed in 0.6
, and do refactoring/rebenchmarking if necessary from time to time — I recognize that 0.7
and especially 1.0
are in flux at the moment.
However, it would be great to receive some preliminary guidance about what the compiler can deal with (and still emit optimized code) in for 0.6
and 0.7
when it comes to Union{T1, T2, ..., Tn}
return types. I did my homework, and read through a lot of github discussions, but I am still confused, and finding things out empirically. Particularly,
-
what are the restrictions on
n
? I found that evenn=3
works occasionally,n=2
, reliably. -
what are the restrictions on the
T
s? They would of course have to be concrete types, but do they have to be bits types? Any other restrictions? -
The above questions are about return types, where the caller resolves the uncertainty immediately (maybe that’s why it is fast for me?) My impression is that
Union
types as fields or eltypes are still not recommended. Is this correct?