fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

assigning choice to choice with additional choices, should the compiler help here?

Open michaellilltokiwa opened this issue 1 year ago • 2 comments

The following currently results in an error but could be resolved automatically by the compiler since the choice that is being assigned to is wider. Is this a good idea? I don't know...

ex is

  success is
  end_of_file is
  my_error is

  d choice success end_of_file := success

  e choice success end_of_file my_error := d
/home/sam/playground/test.fz:9:3: error 1: Incompatible types in assignment
  e choice success end_of_file my_error := d
--^
assignment to field : 'ex.e'
expected formal type: 'choice ex.this.success ex.this.end_of_file ex.this.my_error'
actual type found   : 'choice ex.this.success ex.this.end_of_file'
assignable to       : 'choice ex.this.success ex.this.end_of_file'
for value assigned  : 'd'
To solve this, you could change the type of the target 'ex.e' to 'choice ex.this.success ex.this.end_of_file' or convert the type of the assigned value to 'choice ex.this.success ex.this.end_of_file ex.this.my_error'.

one error.

michaellilltokiwa avatar Mar 25 '24 13:03 michaellilltokiwa

The explicit code to do this would be

  e choice success end_of_file my_error := match d
    s success => s
    e end_of_file => e 

is a little clumsy, but an alternative for the developer would have been using nested choice types as follows

ex is

  success is
  end_of_file is
  my_error is
  normal_result : choice success end_of_file is
  error_result : choice normal_result my_error

  d normal_result := success

  e error_result := d

I would opt for no automatic widening of choice type values for now and to collect experience with such scenarios and find cases where this might be useful and the solution using nested choice types would not work well.

fridis avatar Mar 25 '24 13:03 fridis

kind of related to #1568

michaellilltokiwa avatar Mar 25 '24 14:03 michaellilltokiwa