-
Notifications
You must be signed in to change notification settings - Fork 138
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
Comments
Good idea, this could be easily added, PRs are very welcome.
|
I can take a stab at this, can something like generic |
@j1010001 @bluesign @turbolent is this issue is resolved ? if yes humble request please can we mark it as closed ? |
No, this feature has not been added yet |
I believe we have two options: Option 1We 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 2Declare 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 |
Another option: And .asVariableSizeArray(): T to fixed sized arrays Ofc naming can be improved. |
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. For example, Rust has a generalized way of implementing conversions with the |
@turbolent Sounds good. Regarding the 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]>() |
Correct, |
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.
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.
The text was updated successfully, but these errors were encountered: