"Possible unsafe operation" with Nullable<TDateTime>

121 views
Skip to first unread message

stuartc...@gmail.com

unread,
Dec 4, 2020, 6:12:57 AM12/4/20
to Spring4D
Hi,

I'm new to Spring4D and I'm trying code shown

// minimal viable reproducable test case....
var
  NDate: Nullable<TDateTime>;
  aDate, aTime: TDateTime;
begin
  try
    aDate := EncodeDate(2020,06,02);
    aTime := EncodeTime(11, 5, 0, 0);

    NDate := aDate;
    NDate := NDate.Value + aTime; // compiler warning

The compiler  (10.3)  is giving me:

[dcc32 Warning] Project31.dpr(19): W1000 Symbol '&op_Implicit' is deprecated: 'Possible unsafe operation involving implicit Variant conversion - use explicit cast'

My company is tough on compiler warnings so I'd like to know a) if I'm doing that correctly and b) if so, should I remove that compiler warning? 

Thanks in advance.


PS The original code is getting dates/times from a TDataset so I need to conditionally add the time value if the field is not null.

Stefan Glienke

unread,
Dec 4, 2020, 9:36:06 AM12/4/20
to Spring4D
Looks like a compiler glitch where it choses the wrong overload and turns the result of the addition into a Variant to pass that to the nullable.
Go to Spring.inc and disable {$DEFINE UNSAFE_NULLABLE} to completely remove these unsafe operators (they were in 1.2 for backwards compatibility and exactly for that reason raised that warning)
When you do so the compiler is forced to use the proper implicit operator overload which takes a TDateTime.
Message has been deleted

Stuart Clennett

unread,
Dec 4, 2020, 10:27:52 AM12/4/20
to Spring4D
Thanks Stefan

Toni Grabmayr

unread,
Jan 20, 2021, 4:00:20 AM1/20/21
to Spring4D
Hi,
I use Nullable<Double> and run into the same warning. When I disable  {$DEFINE UNSAFE_NULLABLE}  in Spring.inc then a explicit MyVar := NULL is not possible any more; I get at runtime an error msg that Variant-Value couldn't be assigned to Double. When I activate the DEFINE it works again. However, I tried then to switch the GIT-branch to <developer> as I read that there were some patches to that - no success as well setting NULL to this kind of variable.

Any ideas, hints, practical advice?

Thanks in advance.

Toni

(Delphi Rio 10.3.3, Win 10/64, 32-bit)

Philipp Schäfer

unread,
Jan 20, 2021, 4:04:32 AM1/20/21
to spri...@googlegroups.com
Hi,

newer version of Nullable<T> should be assigned to nil and no longer NULL as far as I am aware.

Hope that helps.

--
You received this message because you are subscribed to the Google Groups "Spring4D" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spring4d+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/spring4d/ce3cd761-8a0c-4eb8-b2fc-11647fa34b17n%40googlegroups.com.

Toni Grabmayr

unread,
Jan 20, 2021, 5:31:09 AM1/20/21
to Spring4D
Hi, 
thanks for the fast reply! So setting MyVar := DBField.Value (Variant, NULL) not works any more(?) and this should be replaced by DBField.AsFloat. I'll have to test that. I wanted to use Nullable<T> as I in deed need to reflect if a value (even 0) was entered or not,,,

thx

Reply all
Reply to author
Forward
0 new messages