Board index » delphi » RFC: Type inference

RFC: Type inference


2008-06-16 05:33:08 PM
delphi42
Hi guys,
Many fellows around here ( ;-) ) asks about type inference in Delphi.
See 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[:Decimals]; var S: string); //wheeee...!!!
3.) For functions:
function Foo(bar, var baz; aNormalArg: integer):;
...here I am 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.
 
 

Re:RFC: Type inference

m. Th. writes:
Quote
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[:Decimals]; var S: string); //wheeee...!!!

...and of course the /very/ interesting one:
Copy(S: <string or dynamic array>; Index: integer; Count: integer);
Sorry, but I couldn't resist... :-)
Being serious again, what about a conditional type inference as above?
But I think now that we are way better with overloaded procs/funcs in
the above case...
--
m. th.
 

Re:RFC: Type inference

m. Th. a écrit :
Will you please refrain from posting in HTML and only use plain text ?
Thank you
Joanna
Joanna Carter [TeamB]
Consultant Software Engineer
 

Re:RFC: Type inference

"Joanna Carter" <XXXX@XXXXX.COM>writes
Quote

Will you please refrain from posting in HTML and only use plain text ?
It looks like plain text and you can not even post using html on this server
(I just tried in the test group).
What makes you think it is html?
 

Re:RFC: Type inference

Joanna Carter writes:
Quote
Will you please refrain from posting in HTML and only use plain text ?
Huh ?!?
--
Andy Syms
Technosoft Systems Ltd
 

Re:RFC: Type inference

m. Th. writes:
Quote
Hi guys,

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

The proposed syntax would be:

1.) For variables:

var
foo; //inferenced

*eek* o_O
Inference makes sense when you see the assignment.
The actual type is not as important as seeing which value it will hold.
Putting this in the preceding var-block w/o any clue of what it will hold makes it an efficient tool
for manual code obfuscation...
Quote

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

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

3.) For functions:

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

....here I am 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?

Completely nonsensical.
Really, how's that gonna work, hmm?
In Oxygene, one could simulate it with generic parameters and generic parameter inference:
method MyClass.Foo<T>(bar : T; var baz : T; normalArg : Integer);
var x := 1;
instance.Foo(2, var x, 4);
The compiler will then pick the type for T depending on the given parameters.
C# can do roughly the same WRT generic inference.
Quote

Comments? Thoughts?

You have strange ideas... o_O
 

Re:RFC: Type inference

m. Th. writes:
Quote
1.) For variables:

var
foo; //inferenced
a, b, c: string; //normal vars
i, j, k: integer; //more normal vars
...
Type inference simply doesn't fit with Object Pascal's var "section", I
think. To really be useful, inline variable declarations would have to
be added to OP:
procedure Foo(const S: string);
begin
var A := 123; // A: Integer
var B := S; // B: String
end;
Otherwise, your proposed syntax could lead to serious issues:
procedure Foo(const S: string);
var A; B;
begin
// ... many lines of code here ...
if (SomeCondition) then
begin
// ... more lines of code here ...
A := S; // A: String
// ... more lines of code here ...
B := 1; // B: Integer
end
else begin
// ... more lines of code here ...
A := 123; // A: Integer
// ... more lines of code here ...
B := S; // B: String
end;
// How does this work now? Nevermind the compiler,
// the coder would be floored way sooner.
A := A + 200;
B := B + 'bar';
end;
Quote
2.) For procedures:

procedure Foo(bar, var baz; aNormalArg: integer);
And what happens to untyped parameters? "Foo", "const Foo", "var Foo"
and "out Foo" are all valid parameter declarations *right now*.
 

Re:RFC: Type inference

Joanna Carter writes:
Quote
>What makes you think it is html?
See my post in attachments with same subject as this thread.
I've looked at the original message in both XanaNews and Thunderbird,
and also viewed the message source in TB. That ain't HTML, despite how
it rendered on your screen.
 

Re:RFC: Type inference

"Joanna Carter" <XXXX@XXXXX.COM>writes
Quote

See my post in attachments with same subject as this thread.
Perhaps your news client recognizes the smiley and changes the format to
html.
 

Re:RFC: Type inference

On Mon, 16 Jun 2008 15:19:17 +0100, Joanna Carter writes:
Quote
>What makes you think it is html?

See my post in attachments with same subject as this thread.
I think that that is your newsreader. It is definitely a plain text
message.
--
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com
 

Re:RFC: Type inference

It turns out that supporting type inferencing in publicly visible
types makes the inferencing much, much harder for the compiler. That's
why C#supports it for local variables only. Start there.
--
Craig Stuntz [TeamB] ?Vertex Systems Corp. ?Columbus, OH
Delphi/InterBase Weblog : blogs.teamb.com/craigstuntz
IB 6 versions prior to 6.0.1.6 are pre-release and may corrupt
your DBs! Open Edition users, get 6.0.1.6 from mers.com
 

Re:RFC: Type inference

Joanna Carter writes:
Quote
m. Th. a écrit :

Will you please refrain from posting in HTML and only use plain text ?
He posted plain text:
Content-Type: text/plain; charset=ISO-8859-7; format=flowed
--
Rudy Velthuis [TeamB] www.teamb.com
"Learning is what most {*word*62}s will do for a living in the 21st
century." -- Perelman
 

Re:RFC: Type inference

Uffe Kousgaard writes:
Quote
"Joanna Carter" <XXXX@XXXXX.COM>writes
news:48567664$XXXX@XXXXX.COM...
>
>See my post in attachments with same subject as this thread.

Perhaps your news client recognizes the smiley and changes the format
to html.
Some newsreaders (say, Thunderbird <g>) don't have to convert to HTML
to do that. They display smileys like that.
--
Rudy Velthuis [TeamB] www.teamb.com
"The difference between {*word*40}ography and {*word*115}a is lighting."
-- Gloria Leonard
 

Re:RFC: Type inference

Craig Stuntz [TeamB] writes:
Quote
Rudy Velthuis [TeamB] writes:

>>var
>>foo; //inferenced
>
>That can't be inferenced, since the compiler has no idea what type
>foo should be.

Well, not if the routine ends there. Presumably, however, there
would be code following this, and the type could be inferred from
that.
Well yes, but I think that is too far away from the declaration. That
is almost dynamic typing (i.e. runtime typing, instead of compile time
typing).
--
Rudy Velthuis [TeamB] www.teamb.com
"The wit makes fun of other persons; the satirist makes fun of
the world; the humorist makes fun of himself."
-- James Thurber (1894-1961),
in Edward R. Murrow television interview
 

Re:RFC: Type inference

Rudy Velthuis [TeamB] writes:
Quote
>var
>foo; //inferenced

That can't be inferenced, since the compiler has no idea what type
foo should be.
Well, not if the routine ends there. Presumably, however, there would
be code following this, and the type could be inferred from that.
--
Craig Stuntz [TeamB] ?Vertex Systems Corp. ?Columbus, OH
Delphi/InterBase Weblog : blogs.teamb.com/craigstuntz
Want to help make Delphi and InterBase better? Use QC!
qc.borland.com -- Vote for important issues