Feature: Ability to specify and cast types #33

Closed
opened 2025-09-08 21:16:28 +00:00 by Furkan · 3 comments

It would be good to have a way to specify the type of a variable like

a : int8 = 1

Also a casting function would be great too so you can have array of integeres

Currently mem.alloc only returns a string you cant manipulate outside of the scope

fizz(arr string) {
 arr[1] = 'b' // Doesnt work
}

main() {
 arr := mem.alloc(8)
 arr[0] = 'a' // Works
 fizz(arr)
 mem.free(arr)
}

It would be good to have a way to specify the type of a variable like a : int8 = 1 Also a casting function would be great too so you can have array of integeres Currently mem.alloc only returns a string you cant manipulate outside of the scope fizz(arr string) {  arr[1] = 'b' // Doesnt work } main() {  arr := mem.alloc(8)  arr[0] = 'a' // Works  fizz(arr)  mem.free(arr) }
ed self-assigned this 2025-09-09 07:54:57 +00:00
Owner

Added in 00d014155d.

Notes:

  • There are currently no safety checks. Here's a very hacky int array.
  • There is no type notation to express certain types yet (e.g. array of type T, or function pointers).
Added in https://git.urbach.dev/cli/q/commit/00d014155d60e6b856aa6738b6c9ce66f73f5a85. Notes: - There are currently [no safety checks](https://git.urbach.dev/cli/q/src/commit/0b5ae9b149eba747f009d9c0e65910bf80b8e39f/tests/type-cast.q#L5-L10). Here's a very hacky [int array](https://git.urbach.dev/cli/q/src/commit/0af6b20648c1d9b81692a4f3e9279f926b785d65/tests/int-array.q#L6-L14). - There is no type notation to express certain types yet (e.g. array of type T, or function pointers).
ed added spent time 2025-09-09 16:33:51 +00:00
4 hours
Owner

This issue contains 2 independent problems.
I personally prefer it when each problem gets its own issue, but it is what it is.
Here are the solutions:

1.) Casting to types

This should be possible now:

a := 1 as byte

Note that this works with primitives but not with structs.

2.) Not being able to write to allocated strings in another function.

There needs to be some mechanism to determine which writes are safe.
However, I've removed the current restriction because it's too restrictive.

Do you think the issue can be closed or is there anything that you would like to add?

This issue contains 2 independent problems. I personally prefer it when each problem gets its own issue, but it is what it is. Here are the solutions: 1.) Casting to types This should be possible now: ``` a := 1 as byte ``` Note that this works with primitives but not with structs. 2.) Not being able to write to allocated strings in another function. There needs to be some mechanism to determine which writes are safe. However, I've removed the current restriction because it's too restrictive. Do you think the issue can be closed or is there anything that you would like to add?
Author

I think issue can be closed, also sorry for saying 2 problems in 1 issue

I think issue can be closed, also sorry for saying 2 problems in 1 issue
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Total time spent: 4 hours
ed
4 hours
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cli/q#33
No description provided.