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

Tuple with choice type choice String (array u8) can not be stored in array #3370

Open
simonvonhackewitz opened this issue Jul 12, 2024 · 2 comments
Labels
bug Something isn't working enhancement New feature or request front end related to the front end until .fum file is created: call and type resolution, type checking, etc.

Comments

@simonvonhackewitz
Copy link
Contributor

Example

ex is
  arr array (tuple (choice String (array u8)) String) :=
    [("foo", "one"),
     ([(u8 65), 66, 67], "two"),
     ("bar", "three")]

  for tup in arr do
    (val, name) := tup
    match val
      str String => say "$name: $str"
      a array u8 => say "$name: {String.type.from_bytes a}"

Error

/tmp/array_choice.fz:5:6: error 1: Incompatible types in array initialization
    [("foo", "one"),

array type          : 'array (tuple (choice String (array u8)) String)'
expected formal type: 'tuple (choice String (array u8)) String'
actual type found   : 'tuple String String'
assignable to       : 'tuple String String'
for value assigned  : '("foo", "one")'
To solve this, you could change the type of the target 'array element' to 'tuple String String' or convert the type of the assigned value to 'tuple (choice String (array u8)) String'.


/tmp/array_choice.fz:6:6: error 2: Incompatible types in array initialization
     ([(u8 65), 66, 67], "two"),

array type          : 'array (tuple (choice String (array u8)) String)'
expected formal type: 'tuple (choice String (array u8)) String'
actual type found   : 'tuple (array u8) String'
assignable to       : 'tuple (array u8) String'
for value assigned  : '([(u8 65), 66, 67], "two")'
To solve this, you could change the type of the target 'array element' to 'tuple (array u8) String' or convert the type of the assigned value to 'tuple (choice String (array u8)) String'.


/tmp/array_choice.fz:7:6: error 3: Incompatible types in array initialization
     ("bar", "three")]

array type          : 'array (tuple (choice String (array u8)) String)'
expected formal type: 'tuple (choice String (array u8)) String'
actual type found   : 'tuple String String'
assignable to       : 'tuple String String'
for value assigned  : '("bar", "three")'
To solve this, you could change the type of the target 'array element' to 'tuple String String' or convert the type of the assigned value to 'tuple (choice String (array u8)) String'.

3 errors.

Without a tuples it works

ex2 is
  arr array (choice String (array u8)) := ["foo", [(u8 65), 66, 67], "bar"]

  for elem in arr do
    match elem
      str String => say str
      a array u8 => say (String.type.from_bytes a)

@simonvonhackewitz simonvonhackewitz added bug Something isn't working front end related to the front end until .fum file is created: call and type resolution, type checking, etc. labels Jul 12, 2024
@fridis fridis added the enhancement New feature or request label Jul 15, 2024
@fridis
Copy link
Member

fridis commented Jul 15, 2024

Here, the type propagation should be improved to propagate the tuple type into the array elements. Given the type explicitly

ex is
  arr array (tuple (choice String (array u8)) String) :=
    [(id (String | array u8) "foo", "one"),
     (id (String | array u8) [65, 66, 67], "two"),
     (id (String | array u8) "bar", "three")]

  for tup in arr do
    (val, name) := tup
    match val
      str String => say "$name: $str"
      a array u8 => say "$name: {String.type.from_bytes a}"

works

> ./build/bin/fz ex3370.fz 
one: foo
two: ABC
three: bar

Eventually, with type propagation enhanced and better pattern matching, this should work:

ex is
  arr :=
    [(id (String | array u8) "foo"       , "one"  ),
     (                       [65, 66, 67], "two"  ),
     (                       "bar"       , "three")]

  for tup in arr do
    match tup
      (str String, name) => say "$name: $str"
      (a array   , name) => say "$name: {String.type.from_bytes a}"

@simonvonhackewitz
Copy link
Contributor Author

This also does not work

test_pairs array (tuple i32 u32):= [(1, 2)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request front end related to the front end until .fum file is created: call and type resolution, type checking, etc.
Projects
None yet
Development

No branches or pull requests

2 participants