I think it would be feasible for vscode-as3mxml to provide a quick fix to
convert the inferred type into an explicitly declared type, if that's what
you mean. I don't think that there's any other tool where it makes sense.
The formatter, for instance, can see the type declarations, but it doesn't
understand the types.

Yes, assigning the variable when it is declared is required (but it doesn't
need to be a literal value... another variable, or even a more complex
expression with operators, function calls, etc. can have their types
detected by the compiler). With no declared type and no assignment, the
variable's value defaults to undefined, which would be inferred as type *
instead.

--
Josh Tynjala
Bowler Hat LLC <https://bowlerhat.dev>


On Tue, Jun 20, 2023 at 2:23 PM Harbs <harbs.li...@gmail.com> wrote:

> Nice.
>
> I personally like explicit types because it’s more human readable.
>
> How feasable would it be to automatically add types to the code based on
> type inference. So var s = “hello” could be transformed to var s:String =
> “hello”.
>
> Do you need to assign the variable when it’s declared for this to work?
>
> > On Jun 21, 2023, at 12:07 AM, Josh Tynjala <joshtynj...@bowlerhat.dev>
> wrote:
> >
> > Hi all,
> >
> > One cool feature in other languages similar to AS3, such as TypeScript or
> > Haxe, is type inference. It allows developers to optionally omit a type
> > declaration from a variable, or a function signature, by having the
> > compiler automatically detect an appropriate type from context.
> >
> > For example, you traditionally would declare a variable with a String
> type
> > like this:
> >
> > var s:String = "hello";
> >
> > If you enable type inference, you can declare the same variable like this
> > instead because the compiler knows that the initializer is a string.
> >
> > var s = "hello";
> >
> > Type inference applies to local/member/static variables, getters/setters,
> > function parameters, and function return values. If a function has
> multiple
> > return statements, the type inference will try to find a common base
> type,
> > if possible. If a type cannot be inferred for any reason, the compiler
> will
> > fall back to the * type, and you'll get a warning, the same as when type
> > inference is disabled.
> >
> > If you build the compiler from source (or download the next nightly
> build),
> > you can enable this feature in your project using the new
> -infer-types=true
> > compiler option. It is opt-in for now, to avoid any potential backwards
> > compatibility issues with existing code.
> >
> > I also wrote some documentation with examples and more detailed
> > explanations:
> >
> > https://apache.github.io/royale-docs/features/as3/type-inference
> >
> > --
> > Josh Tynjala
> > Bowler Hat LLC <https://bowlerhat.dev>
>
>

Reply via email to