Proposal: Type inference for variable definition/initialization using the 'auto' keyword.
per at bothner.com
Thu May 21 10:00:27 PDT 2009
On 05/21/2009 09:45 AM, Tim Lebedkov wrote:
> On Wed, May 20, 2009 at 11:52 AM, Maurizio Cimadamore
> <Maurizio.Cimadamore at sun.com> wrote:
>> often JavaFX users complain about the impossibility of writing code like:
>> var x = "Hello";
>> x = 1; //type-error here, Integer cannot be assigned to String
> In my opinion the JavaFX implementation is better: it is easier to implement and
> more type safe. I'll add this example to the proposal.
The problem is that gets into awkwardness of the "most specific type"
issues. For example:
var x = 0; // Inferred to be integer
x = x + 0.1; // Oops
What we'd like to do is infer the type to be the "union type"
(or most specific shared super-type) of all the assignments to
a variable. This of course requires all the assignments to be
visible, which is why we made the default visibility in JavaFX
"script-local" (rather than package-local as in Java).
An increment as above would normally cause a cycle (because the
variable is used in the RHS of the assignment), but it is
possible to special-case increments (and appending), and that
would seem worthwhile.
(Conditional expressions also use "union types".)
Unfortunately, we haven't had time to implement a better
type inference algorithm.
per at bothner.com http://per.bothner.com/
More information about the coin-dev