Forum > General

mormot2 JSON + If + and = wtf?

<< < (2/2)

Joanna:
Yes that’s the one. Thanks for link.
I wonder what Sorts of things that would be used for.

PascalDragon:

--- Quote from: Joanna on February 07, 2023, 03:33:07 pm ---Yes that’s the one. Thanks for link.
I wonder what Sorts of things that would be used for.

--- End quote ---

If your expression contains function calls then with short boolean evaluation those on the right might not be evaluated depending on the values of those on the left side. Additionally with full boolean evaluation the compiler can reorder the expressions to generate better code. So depending on your usecase there are uses for it.

Joanna:
I’ve become rather dependent upon the Boolean expression exiting after result is known because I write things like
If (x <> nil) And (x.field1 > 20) Then ...

PascalDragon:

--- Quote from: Joanna on February 08, 2023, 12:08:56 am ---I’ve become rather dependent upon the Boolean expression exiting after result is known because I write things like
If (x <> nil) And (x.field1 > 20) Then ...

--- End quote ---

Well, short boolean evaluation is enabled by default after all...

abouchez:
You could have rather used the mORMot 2 forum directly.
https://synopse.info/forum
You would have used your answer sooner.

Your code seems not correct, because I guess you make some wrong assumptions.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---J._(2)<>nilwill always be true I guess. Or at least it is weird/confusing to write such an expression.

J is a variant.
J._() is a variant pseudo-method, also called "late binding", which is evaluated at runtime, and returns a variant value.
I guess you made a confusion between nil (which is a pointer) and null (which is a variant).
So it is never equal to nil at least in FPC - perhaps Delphi makes another assumption but it is rather a very low-level undocumented behavior to compare a variant with a pointer. Perhaps Delphi compare nil into a (void) string, whereas FPC does not. But this is not the point, because it actually compare oranges and apples. ;)

For such complex code, don't use variant and late-binding.
But directly use a TDocVariant local variable. As documented.
This is cleaner, safer, and also faster.
The compiler will check all the types for you - and won't allow any confusion between nil and null for instance.
And you will have much more methods than only the "pseudo methods".

I have observed (at least some years ago) that FPC late-binding support was not as good as Delphi's, and triggered some random GPF.
https://synopse.info/files/html/Synopse%20mORMot%20Framework%20SAD%201.18.html#TITLE_40
(note that the proper documentation about TDocVariant is this URI and not the initial blog article).
So I would not advice use variant late-binding on FPC, but use explicit TDocVariantData values. Then typecast them to a variant if needed.

Casting a mORMot into a variant is mostly useful if you want to store value, and transmit and store them as variants, e.g. from one method to another, mainly for SOA calls.
Rather use TDocVariant whenever you can, if you want to work with JSON.
Using such custom variants in expressions, or casted into other variables (as strings) can be very confusing if you don't know the RTL intrinsics of custom variants types.

Navigation

[0] Message Index

[*] Previous page

Go to full version