Skip to content

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

Closed
@weenzeel

Description

@weenzeel

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions