SEARCH YOUR SOLUTION HERE  

RFC: Type inference

Hi ,

Many fellows around here ( ;-) ) asks about type inference in Delphi.
See http://en.wikipedia.org/wiki/Type_inference for more details.

The proposed syntax would be:

1.) For variables:

var
foo; //inferenced
a, b, c: string; //normal vars
i, j, k: integer; //more normal vars
...

2.) For procedures:

procedure Foo(bar, var baz; aNormalArg: integer);

FTR, we have already some 'oddities' in Delphi related to our theme:

Read([var F: File;] V1, V2, [..VN]); //a true 'magic' declaration :-)
//the same stands for Write

Val(S: string; var V; var Code: integer); //what's the type of 'V'
Str(x: [:Width[ecimals]; var S: string); //wheeee...!!!

3.) For functions:

function Foo(bar, var baz; aNormalArg: integer):;

....here I'm not so sure about the syntax. I put the : at the end to show
that the result is inferenced. But perhaps isn't so readable

Comments Thoughts

--

m. th.

Posted On: Tuesday 16th of October 2012 03:50:15 AM Total Views:  88
View Complete with Replies




Related Messages:

Testing for field type.   (59 Views)
I am trying to write a routine that will test whether a field is string or integer or boolean, etc. Is there a way to do this
Is there a way to get size of typed name List?   (240 Views)
If you have a type tsomeType = (stOne, stAnother, ..., stEtc) I know you can make an array somearray[tsomeType] of whatever, so the compiler obviously knows how many items are in your list. There are times when I'd like to be able to say high(tsometype) or length(tSometype) or something like that, but the compiler will not accept that. Sizeof(tsometype), of course gives the size of the enumerator, not the size of the list. The most frequent need for this is to make sure a value is not outside the range of values but there are also times when I'd like to enumerate over the range of values in the list in a for loop for example. Anyone know of a syntax that will allow me to get the size of the list of enumerators in a given type Or the value of the last item in the list. Hard coding the item (i.e. using the value of the last known item in the list) causes code to break when new items are added. I guess one could always code a dummy item last in the list, but I don't see any evidence that anyone does this. I can also see that one could define a dummy array of tsometype and use that to get the high value. --
Query about TDate data type   (204 Views)
My understanding of the TDatetime datatype is that it is a double precision floating point number in which the integer part defines the date (i.e. the number of days from a reference date) and the fractional part defines the time of day expressed as a fraction of 24 hours. I then see that the TDate datatype, which is supposed to hold a date with no time information, is an alias for TDatetime, and therefore occupies the same memory as a full Datetime value. What, then, is the purpose of providing the separate datatype TDate, when we could just as well use TDatetime for dates, times, and date-times indiscriminately In my case, however, I would like a separate data type TDate1 that occupies the same memory as an integer, defines the date in the same way as the integer part of a TDatetime variable, and occupies half the memory of a TDatetime variable. I would like this type to be freely usable in expressions which perform date and datetime arithmetic and be automatically converted by the compiler to and from the TDatetime type as dictated by the expression.
Multi-dimensional arrays: static dimensions, rectangular versus jagged types, component aliasing   (273 Views)
Hi I am a frequent user of multi-dimensional arrays. They are a great data structure, but there is still scope for improvement! Below I list some deable features that are not AFAIK available in Delphi 7. Perhaps some or all these features are already available in later versions of Delphi. Can anyone advise 1. ocation and resizing of multi-dimensional dynamic arrays containing some static dimensions. The SetLength method for allocating or re-dimensioning a dynamic array leaves something to be deed, because not sufficiently general to handle arrays containing some static dimensions. Consider the following type definitions: type TMyRecord= record Value: integer; {...} end; TDynamicArray1D= array {DimensionSize0} of TMyRecord; TDynamicArray2DA= array {DimensionSize1} of TDynamicArray1D; TDynamicArray3DA= array {DimensionSize2} of TDynamicArray2DA; TDynamicArray2DB= array {[{DimensionSize1=}2} of TDynamicArray1D; {*Static* array type} TDynamicArray3DB= array {DimensionSize2} of TDynamicArray2DB; var DynamicArray3DA: TDynamicArray3DA; DynamicArray3DB: TDynamicArray3DB; begin SetLength(DynamicArray3DA, 10, 6, 3); {This succeeds} SetLength(DynamicArray3DB, 10, 2, 3); {This fails} ... end; Array type TDynamicArray3DA, whose dimensions are all dynamic, can be allocated with a single SetLength call. On the other hand, array type TDynamicArray3DB, which contains a static dimension at level 1, cannot be allocated by a single call to SetLength. One has to write SetLength(DynamicArray3DB, 10); for i:= 0 to 9 do for j:= 0 to 2 do SetLength(DynamicArray3DB[i, j], 3); What we need is another dynamic array allocation method having syntax along the following lines: SizeArray(var DynamicArray, DimensionSizeArray]; where DimensionSizeArray= '[' DimensionSize {',' DimensionSize} ']'; DimensionSize= DynamicDimensionSize | StaticDimensionSize; DynamicDimensionSize= IntegerExpression; StaticDimensionSize= 'S' | ' ' An integer expression is provided for each dynamic dimension, whilst the character 'S' or ' ' is provided for each static dimension. Thus the sizing statement for the above array could be written SizeArray(DynamicArray3DB, [10, S, 3]); {S stands for 'Static dimension'} or SizeArray(DynamicArray3DB, [10, , 3]); 2. Differentiation between rectangular and jagged arrays. C# differentiates between rectangular and jagged arrays. Delphi D7 doesn't, as far as I am aware. It would be deable to support both cases and if necessary extend the SizeArray method to handle both cases. 3. Extending the "with" statement to alias array component references. To avoid unnecessary repeated multiple subscripting it would be deable to have an extended 'with' syntax along the following lines: for i:= 0 to High(DynamicArray3DB) do with DI as DynamicArray3DB[i] do {DI is an alias for DynamicArray3DB[i]} begin SI:= i*100; for j:= 0 to High(DI) do with DIJ as DI[j] do begin SIJ:= SI + j*10; for k:= 0 to High(DIJ) do begin with DIJ[k] do begin {.}Value:= SIJ + k; end; end; end; end; If only reading array component values, one can avoid unnecessary multiple subscripting by using separate variables pointing to dynamic arrays as follows: var DynamicArray3DA: TDynamicArray3DA; DI: TDynamicArray2DA; DIJ: TDynamicArray1D; begin SetSizeArray(DynamicArray3DA, [10, 5, 3]); for i:= 0 to High(DynamicArray3DA) do begin DI:= DynamicArray3DA[i] ; SI:= i*100; for j:= 0 to High(DI) do {etc.} This approach cannot be used efficiently when writing array component values, because of Delphi's copy on modify policy. For writing one has to use: for i:= 0 to High(DynamicArray3DB) do with DI as DynamicArray3DB[i] do {DI is an alias for DynamicArray3DB[i]} begin SI:= i*100; for j:= 0 to High(DI) do begin DIJ:= DI[j]; SIJ:= SI + j*10; for k:= 0 to High(DIJ) do begin with DIJ[k] do begin {.}Value:= SIJ + k; end; end; DI[j]:= DIJ; {Must copy modified array back to parent array component!} end; end;
movable type or wordpress blog   (106 Views)
so which is better -- Dennis Landi http://www.dennislandi.com ied Data, Inc. http://www.allied-data.com Electron Server http://www.electronserver.com
Is that possible to use function to return a procedural type?   (172 Views)
I have a procedural type declare as: type TMyFunc = function(const a, b: integer): integer; And I have 2 functions: function Add(const a, b: integer): integer; begin Result := a + b; end; function Substract(const a, b: integer): integer; begin Result := a - b; end; I then declare a procedure to return one of function above: procedure GetProc(const aFlag: boolean; out aProc: TMyFunc); begin if aFlag then aProc := Add else aProc := Substract; end; I may then use the GetProc as: var P: TMyFunc; i: integer; begin GetProc(True, P); i := P(10, 15); end; The above code runs without as expected. However, if I want to change the GetProc to function that return TMyFunc: function GetFunc(const aFlag: boolean): TMyFunc; begin if aFlag then aProc := Add else aProc := Substract; end; And use it as: i := GetFunc(True)(10, 15); I get a compilation error: "E2066 Missing operator or semicolon". I may only use it as: i := TMyFunc(GetFunc(True))(10, 15); or var P: TMyFunc; i: integer; begin P := GetFunc(True); i := P(10, 15); end; Is this the correct way of using the function to return a procedural type Thank you. -- Best regards, Chau Chee Yang E Stream Software Sdn Bhd URL: www.sql.com.my SQL Financial Accounting
How to reraise exception of same type with new message??   (205 Views)
How to reraise exception of same type with new message Like: try Result := 15/0; except on E:Exception do raise E.Create('Are you nuts!! :' + E.Message); end; (code Above does not work, I kind of hoped so ) I want to have exactly same exception reraised like plain except raise; end; But I need to add information to the Exception message. -TP-
assign string value to a property of a set type   (106 Views)
, - I have a string value for example 'ST1' and I have an object property of type set (or enumeration I am not sure) called Sys (type TSys) has those values [pST1, pST2, pST3] is there a way to assign the string value to the set other than using IF..ElseIF - Please also advice me in what cases I can use this typcast syntax : ({string type} as TAnotherClass)
typed constants - just curious   (128 Views)
I would appreciate the following construction: type TColor = array[0..2] of Real; const red: TColor = (1.0, 0.0, 0.0); green: TColor = (0.0, 1.0, 0.0); blue: TColor = (0.0, 0.0, 1.0); black: TColor = (0.0, 0.0, 0.0); NiceColors: array[0..1] of TColor = (red, green); (*) UglyColors: array[0..1] of TColor = (blue, black); (*) What is the reason that the compiler complains at the lines marked with (*). I know that the typed constants are not true constants but I find this construction quite handy - or maybe it's fatally dangerous construction jindra , Jindra Parus wrote: > I would appreciate the following construction: > > type > TColor = array[0..2] of Real; > > const > red: TColor = (1.0, 0.0, 0.0); > green: TColor = (0.0, 1.0, 0.0); > blue: TColor = (0.0, 0.0, 1.0); > black: TColor = (0.0, 0.0, 0.0); > NiceColors: array[0..1] of TColor = (red, green); (*) > UglyColors: array[0..1] of TColor = (blue, black); (*) > > What is the reason that the compiler complains at the lines marked > with (*). Because blue should be in NiceColors. Seriously: the compiler needs true constants, i.e. constants that are not typed, there. It will not take typed constants, even if this were possible, in reality. If you want that, you'll have to make NiceColors and UglyColors variables and fill them in code: NiceColors[0] := red; NiceColors[1] := green; etc... -- Rudy Velthuis [TeamB] http://www.teamb.com "A poem is never finished, only abandoned." -- Paul Valery (1871-1945)
[About String type] size and contiguously   (59 Views)
I want know how many data i can keep in one String variable(to keep contiguously). I know that Long string (in Delphi's help AnsiString) represents a dynamically allocated string whose maximum length is limited only by available memory. 32-bit Delphi versions use long strings by default. But its impossible that delphi memory manager find 2GB for my String variable contiguously in my RAM.i think that String memory in RAM can be fragmentary and if i iterate my String variable in a loop with a pointer addressing, im in wrong, because address of S[i+1] is not S[i]+SizeOf(byte) .. I need more info about String type, im sorry for following question but is important for me : 1.Can we promise that if we assign 2GB characters to a String variable, it keep all contiguously in RAM 2.Is String a hidden or predefined class or its only a data type that delphi compiler are managing some operators for it 3.Where can i get Reference counting, copy-on-write info about one string
Winsock types..packet record   (41 Views)
"Mike Collins" wrote in message news:47fd5136$1@newsgroups.borland.com... > the only difference seems to be the packet record. I > only ask because i'm chasing a exception thats being > raised and i was wondering if that might be the issue. Not likely, since the record fields are aligned on WORD boundaries, which works fine in a DWORD-aligned environment, like the VCL. But you can use the debugger to verify the structure packing actualy being used by the compiler. It is usually not a bad idea to use 'packed' when dealing with API structures. Gambit
How not to make types used in objects private part public   (64 Views)
I've got this old code, that looks more or less like this: unit FooUnit; interface uses .... type LocalRecordType = record Tx: double; Ty: double; end; type TMyObject = class(TMyParentObject) private m_LRT: LocalRecordType; public Constructor Create; Destructor Destroy; override; function DoSomething() : boolean; end; implementation ..... Now with the code like this LocalRecordType is publicly available to all other units where I include/use/import this unit. But I am only using the type locally in TMyObject. So this really, ahum, sucks since I get name-collisions, and worse it is not good information hiding. So how could I solve this I solved the name-collision by renaming 'LocalRecordType' in this module. But I still think it's like 'porn' coding such a way a local used type becomes publicly available. (BTW, I have more experience in Java and C++, then Delphi, so it could help if your answer is more or less in Java jargon.)
Getting icon for file type   (64 Views)
I need to get the icons for different file types. At the moment I am creating an empty file with the deed extension and calling ShGetFileInfo() But that seems very long winded and I don't like writing to the disk unnecessarily. Is there a better way to get it just using the extension
Selecting multiple controls by their type   (110 Views)
Is there away to select multiple controls on a form (data module) that are all of the same type (class). I have too many controls to shift-select only the ones that I'm interested into. I want to change a value of a property all those controls share. p.s. Imagine a data module with hundreds of TTable (TDataSource, ...) I need to select ALL TTable components. -rascal
String in some type of list   (87 Views)
I have a list of filenames, and I want to see if the current filename I have matches any of the names in the list. I'm unsure of how to create a TStringList as a constant, can it even be done If not I might have to use an array. But with both an array and TStringList what function/method could I use to see if my current string was in the list. - Dan
cast integer to enumerated type?   (43 Views)
Is there an inverse of the Ord function I want to assign an integer to enumerated type, but I can't see how. Do I have to get involved with pointers to do this
office2007 type library   (67 Views)
hi, I have d7 and office 2007, and when i run app. which send reports to excel or word an error of old format or invalid type library is raised, why, whats the file i have to include in uses clause(i have excel2000 on uses clause) to work with of2007 Whats the best gif library, i'm using tgifimage, but it doesn't show all .gif images on the app. Using d7
How not to make types used in objects private part public   (74 Views)
I've got this old code, that looks more or less like this: unit FooUnit; interface uses .... type LocalRecordType = record Tx: double; Ty: double; end; type TMyObject = class(TMyParentObject) private m_LRT: LocalRecordType; public Constructor Create; Destructor Destroy; override; function DoSomething() : boolean; end; implementation ..... Now with the code like this LocalRecordType is publicly available to all other units where I include/use/import this unit. But I am only using the type locally in TMyObject. So this really, ahum, sucks since I get name-collisions, and worse it is not good information hiding. So how could I solve this I solved the name-collision by renaming 'LocalRecordType' in this module. But I still think it's like 'porn' coding such a way a local used type becomes publicly available. (BTW, I have more experience in Java and C++, then Delphi, so it could help if your answer is more or less in Java jargon.)
Duplicate types?   (51 Views)
Two third-party components in my app use the same type name for something. Can I resolve this somehow without renaming one of the types in the original source -- George Newton
[OT] content-type HTML POST   (54 Views)
Hi all, Hope is having (or had) a great weekend. I'm writing a HTTP server to handle simple requests from clients on a local network. Each client will submit a POST to the HTTP server and include a base64 encoded encrypted packet to be processed by the server. I'm curious if the content-type for the post should be "application/x-www-form-urlencoded" and since the server response will also be base64 encoded encrypted packet, what that content-type should be