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

Allow conversion between constant-size and variable-size arrays #2530

Closed
Tracked by #1972
bluesign opened this issue Jun 5, 2023 · 9 comments · Fixed by #3028
Closed
Tracked by #1972

Allow conversion between constant-size and variable-size arrays #2530

bluesign opened this issue Jun 5, 2023 · 9 comments · Fixed by #3028

Comments

@bluesign
Copy link
Contributor

bluesign commented Jun 5, 2023

Issue to be solved

Inspired by recent issue on codeHash in events, I think we should allow some conversion with arrays of different static type.

// https://run.dnz.dev/snippet/1f8bfbdc77297319

pub fun main(){
    var a : [UInt8; 8] = [1,2,3,4,5,6,7,8]
    var b : [UInt8] = [10,11,12,13,14,15,16,17,18]

    var c : [UInt8] = a as! [UInt8]
    var d : [UInt8; 8] = b as! [UInt8; 8]
}

if we don't allow this, only solution is ugly loop.

Suggested Solution

Conversion can allow ( with force cast for example ) conversion between 2 types. As force cast has T as right side, I don't think it will create any problems.

@turbolent
Copy link
Member

turbolent commented Jun 5, 2023

Good idea, this could be easily added, PRs are very welcome.

as/as?/! are casting operators and do not perform conversion. Instead, the standard library offers conversion functions. The same should be done here, though naming it might be a bit tough.

@bluesign
Copy link
Contributor Author

bluesign commented Jun 5, 2023

Good idea, this could be easily added, PRs are very welcome.

as/as?/! are casting operators and do not perform conversion. Instead, the standard library offers conversion functions. The same should be done here, though naming it might be a bit tough.

I can take a stab at this, can something like generic convert<T>(AnyStruct): T work ? Do we have conversion somehow behind the scenes?

@j1010001 j1010001 added the Good First Issue Good for newcomers label Jun 16, 2023
@PratikDhanave
Copy link
Contributor

@j1010001 @bluesign @turbolent is this issue is resolved ? if yes humble request please can we mark it as closed ?

@turbolent
Copy link
Member

No, this feature has not been added yet

@darkdrag00nv2
Copy link
Contributor

I believe we have two options:

Option 1

We could introduce names for the variable and constant sized array types. Then we could declare conversion functions

VariableSizedArray.fromConstantSizedArray<C: [T; _]>(param: C) : [T]
ConstantSizedArray.fromVariableSizedArray<C: [T; _]>(param: [T]) : C

Option 2

Declare two standard library functions:

VariableSizedArrayToConstantSizedArray<C: [T; _]>(param: [T]) : C // or ConstantSizedArrayFromVariableSizedArray
ConstantSizedArrayToVariableSizedArray<C: [T; _]>(param: C) : [T] // or VariableSizedArrayFromConstantSizedArray

In either of these two options, I am not sure if it would be feasible to add the type bound C : [T; _].

@bluesign
Copy link
Contributor Author

bluesign commented Jan 3, 2024

Another option:
.asFixedSizeArray(): T to variable sized arrays.

And .asVariableSizeArray(): T to fixed sized arrays

Ofc naming can be improved.

@turbolent
Copy link
Member

turbolent commented Jan 4, 2024

Agreed, it might be better to not "pollute" the global namespace and just have functions on the types.

Maybe something short that omits the result kind (array) will be sufficient, given that the source is the same kind, e.g. toConstantSized and toVariableSized.

For example, Rust has a generalized way of implementing conversions with the TryInto trait / the try_into function (e.g. see https://stackoverflow.com/a/37669259/2779550).
Similarly, maybe the functions could/should return optionals so that a failure can be handled?

@turbolent turbolent changed the title Allow conversion between fixed size and dynamic size arrays Allow conversion between constant-size and variable-size arrays Jan 5, 2024
@darkdrag00nv2
Copy link
Contributor

darkdrag00nv2 commented Jan 5, 2024

@turbolent Sounds good.

Regarding the toConstantSized function on the VariableSizedType, it will need a type parameter to statically get the size of the constant sized array.

let intArray = [1, 2, 3]
let constArray = intArray.toConstantSized<[Int; 3]>()  // expected usage

// checker will allow this since we don't know the size of array statically but will lead to a runtime error or return a Nil optional value if we make the function return an Option.
let constArray2 = intArray.toConstantSized<[Int; 2]>() 

@turbolent
Copy link
Member

Regarding the toConstantSized function on the VariableSizedType, it will need a type parameter to statically get the size of the constant sized array.

Correct, toConstantSized will need a type parameter, and toVariableSized doesn't 👍

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

Successfully merging a pull request may close this issue.

5 participants