Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type promotion for non-local variables using the is operator #2397

Closed
Sander0542 opened this issue Aug 9, 2022 · 2 comments
Closed

Type promotion for non-local variables using the is operator #2397

Sander0542 opened this issue Aug 9, 2022 · 2 comments
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists

Comments

@Sander0542
Copy link

The is operator in dart can check whether an instance is a type of another type.

int a = 1;
Object b = a;

b is int // true

When using local variables of the type Object there is automatic type promotion, but when the instance of the object is anything else there is no promotion and the use of the as operator is needed to cast.

Other languages like C# support casting inside the is operator.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns#declaration-and-type-patterns

object greeting = "Hello, World!";
if (greeting is string message)
{
    Console.WriteLine(message.ToLower());  // output: hello, world!
}

A feature like this would simplify the code and removes unnecessary lines that don't add anything but clutter.

An example implemenation

int a = 1;
Object b = a;

if (b is int c) {
  // within this block c is definced as int
}
@Sander0542 Sander0542 added the feature Proposed language feature that solves one or more problems label Aug 9, 2022
@lrhn
Copy link
Member

lrhn commented Aug 9, 2022

This looks like a duplicate of "Binding Cast and Check" in #1191.
There might be other similar proposals too.

@Levi-Lesches
Copy link

@lrhn lrhn closed this as completed Aug 10, 2022
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants