std.data.json formal review

Sönke Ludwig via Digitalmars-d digitalmars-d at puremagic.com
Sat Aug 22 07:14:45 PDT 2015


Am 21.08.2015 um 18:56 schrieb Andrei Alexandrescu:
> On 8/18/15 1:21 PM, Sönke Ludwig wrote:
>> Am 18.08.2015 um 00:37 schrieb Andrei Alexandrescu:
>>> On 8/17/15 2:56 PM, Sönke Ludwig wrote:
>>>> - The enum is useful to be able to identify the types outside of the D
>>>> code itself. For example when serializing the data to disk, or when
>>>> communicating with C code.
>>>
>>> OK.
>>>
>>>> - It enables the use of pattern matching (final switch), which is often
>>>> very convenient, faster, and safer than an if-else cascade.
>>>
>>> Sounds tenuous.
>>
>> It's more convenient/readable in cases where a complex type is used
>> (typeID == Type.object vs. has!(JSONValue[string]). This is especially
>> true if the type is ever changed (or parametric) and all has!()/get!()
>> code needs to be adjusted accordingly.
>>
>> It's faster, even if there is no indirect call involved in the pointer
>> case, because the compiler can emit efficient jump tables instead of
>> generating a series of conditional jumps (if-else-cascade).
>>
>> It's safer because of the possibility to use final switch in addition to
>> a normal switch.
>>
>> I wouldn't call that tenuous.
>
> Well I guess I would, but no matter. It's something where reasonable
> people may disagree.

It depends on the perspective/use case, so it's surely not unreasonable 
to disagree here. But I'm especially not happy with the "final switch" 
argument getting dismissed so easily. By the same logic, we could also 
question the existence of "final switch", or even "switch", as a feature 
in the first place.

Performance benefits are certainly nice, too, but that's really just an 
implementation detail. The important trait is that the types get a name 
and that they form an enumerable set. This is quite similar to comparing 
a struct with named members to an anonymous Tuple!(T...).


More information about the Digitalmars-d mailing list