Re: [fpc-pascal] Option type
denisgolovan via fpc-pascal schrieb am Mi., 2. Juni 2021, 13:28: > > > > Well as already discovered type like strings can not go into a "record > case" > > > > But... The above record is anyway of constant size. I.e. the memory for > > the field is always included, even if it is not used. > > > > Since the "false" block is empty, you can do > > > > type > > generic TOption = record > > IsSome:boolean; > > some: T; > > end; > > > > It is not as expressive to the reader of the code. But it leads to the > > same data in memory. > > Yes. > Except that "some" is still initialized with something (some default) and > dropped and copied and assigned despite conceptually being empty. > That would have to be the case otherwise as well. And even worse: the RTL would need to decide at runtime if it would need to manage that entry. With the suggested solution (which is essentially what TNullable<> provides as well) this will be rather cheap if the element is Nil (aka empty) anyway. Regards, Sven > ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Option type
> Well as already discovered type like strings can not go into a "record case" > > But... The above record is anyway of constant size. I.e. the memory for > the field is always included, even if it is not used. > > Since the "false" block is empty, you can do > > type > generic TOption = record > IsSome:boolean; > some: T; > end; > > It is not as expressive to the reader of the code. But it leads to the > same data in memory. Yes. Except that "some" is still initialized with something (some default) and dropped and copied and assigned despite conceptually being empty. -- Regards, Denis Golovan ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
[fpc-pascal] is this a bug?
I am using FPC v. 3.2.0 on both linux and windows. I use Firebird as a database (not sure that this is important). Say you write var AConnection:TIBConnection; AQuery:TSQLQuery; ATran:TSQLTransaction; begin // create the components... // open a database... // prepare a first query... ATran.SQLConnection := AConnection; AQuery.SQLConnection := AConnection; AQuery.Transaction := ATran; AQuery.SQL.Text:= ' select 1 '+ ' from tb_dpt '+ ' where cod_dpt=:deviceId '; // assign parameters... AQuery.Open; // this query returns 1 // ... // other code here... // ... // start a new query AQuery.SQL.Text:= ' select id_log'+ ' from InsertIntoA_LOG('+ ' :ACTION ,'+ ' :USERNAME ,'+ ' :MAC,'+ ' :ATEXT) '; // prepare the query... // assign parameters... AQuery.Open; // a GUID is expected here as a result. Instead, the query returns 1! GULP! < The query uses the old SQL! No error is raised when I assign the new parameters! In order to get the expected results, I have to explicitly close the query before re-assigning AQuery.SQL.Text . I am sure that this did not happen in Delphi. The compiler should forbid the query to be reassigned when open, or it should authomatically close it then reassign it Or at least an error should be raised when I try to assign say parameter :ACTION is the 2nd query... Not sure about the right path to take, but this behaviour brought me alread a lot of hair-pulling :) Peppe Polpo ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Option type
On 01/06/2021 22:40, Sven Barth via fpc-pascal wrote: Am 01.06.2021 um 20:20 schrieb denisgolovan via fpc-pascal: I am trying to implement Option type in FPC. type generic TOption = record case IsSome:boolean of true: ( some: T ); false: (); end; Well as already discovered type like strings can not go into a "record case" But... The above record is anyway of constant size. I.e. the memory for the field is always included, even if it is not used. Since the "false" block is empty, you can do type generic TOption = record IsSome:boolean; some: T; end; It is not as expressive to the reader of the code. But it leads to the same data in memory. There is only a diff, if the other blocks of the record (the false block) also has/have data. With case the memory will overlap. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal