Skip to content

Introduce fix keyword as syntactic sugar for local, promotable, nullable field #1980

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

Closed
weenzeel opened this issue Nov 21, 2021 · 3 comments
Closed
Labels
feature Proposed language feature that solves one or more problems

Comments

@weenzeel
Copy link

I've been reading the comments about if-variables.

What if we introduced a "fix" keyword, so that we could do ...

class Coffee {
  String? temperature;

  void heat() { temperature = 'hot'; }
  void chill() { temperature = 'iced'; }

  void checkTemp() {
    
    fix temperature;
    
    if (temperature != null) {
       print('Ready to serve ' + temperature + '!');
    } else {
      temperature = 'UNKNOWN';
    }


    // If we want the unfixed value we can just refer to it via this.temperature
        
  }

  String serve() => temperature! + ' coffee';
}

The code above would be equal to ...

class Coffee {
  String? temperature;

  void heat() { temperature = 'hot'; }
  void chill() { temperature = 'iced'; }

  void checkTemp() {
    
    var fixedTemperature = temperature;
    
    if (fixedTemperature != null) {
       print('Ready to serve ' + fixedTemperature + '!');
    } else {
      fixedTemperature = 'UNKNOWN';
      temperature = 'UNKNOWN';
    }
  }

  String serve() => temperature! + ' coffee';
}
  • Fixing a field is like creating a local copy at a fixed point in time. That copy persists until the end of the scope.
  • Writing to a fixed field is like updating a local copy in sync with the original field.
  • The unfixed field can always be fetched via this.field.
  • A fixed field can be refixed to refresh the fixed value.

Just throwing it out there :)

@weenzeel weenzeel added the feature Proposed language feature that solves one or more problems label Nov 21, 2021
@mateusfccp
Copy link
Contributor

This seems to be very similar to #1514.

@weenzeel
Copy link
Author

This seems to be very similar to #1514.

@mateusfccp You're right. It's basically the same idea, just a different keyword. Thanks for making the connection!

@mit-mit
Copy link
Member

mit-mit commented Nov 22, 2021

Closing as a duplicate of #1514

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
Projects
None yet
Development

No branches or pull requests

3 participants