Skip to content

Instantly share code, notes, and snippets.

@ro6
Created February 26, 2017 13:23
Show Gist options
  • Save ro6/325f3d9013ab57b43cd760798d0a4dee to your computer and use it in GitHub Desktop.
Save ro6/325f3d9013ab57b43cd760798d0a4dee to your computer and use it in GitHub Desktop.
Trying to create an empty array with space for at least 1 element, add an element at position 0, then retrieve the element
actor Main
new create(env: Env) =>
let arr: Array[Bool] = Array[Bool](1)
try
arr(0) = true
env.out.print(arr(0).string())
else
env.out.print("fail")
end
@SeanTAllen
Copy link

What you are looking for in this case is...

actor Main
  new create(env: Env) =>
    let arr = Array[(Bool|None)].init(None, 10)

    try
      arr.insert(8, true)
      env.out.print(arr(8).string())
    else
      env.out.print("fail")
    end
actor Main
  new create(env: Env) =>
    let arr: Array[Bool] = Array[Bool](1)

    try
      arr.insert(0, true)
      env.out.print(arr(0).string())
    else
      env.out.print("fail")
    end

If you note from the Array constructor...

    """
    Create an array with zero elements, but space for len elements.
    """

And from update that you were trying to use

    """
    Change the i-th element, raising an error if the index is out of bounds.
    """

If you were using numbers, you could use undefined but that isn't.

From a type perspective, the Bool or None that I use is what you are trying to define here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment