crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.47k stars 1.62k forks source link

Slices and memory allocation as well methods to read and write inside structs. #14141

Closed UltimaTerra closed 10 months ago

UltimaTerra commented 10 months ago

"If you give a monkey a hammer, everything looks like a nail.' - Me probably

So uhm, happy holidays! I am working with slices. I know the title is vague, this is a general discussion on what I am doing.

So I am currently just having fun crystal, coming from ruby. Not too relevant, but I have been mostly interested in the Slice type.

So, below I have a rather janky way to store complex numbers, yes I know I can use the built in complex numbers, but I was on slices in the standard library out of intrigue and I sort of jotted the idea as a quick and dirty back of the napkin code piece. Just an experiment, but the idea I sorted jotted down was saving positive and negative ints. I imagined since my general project right now is a game engine with SDL bindings, C and a lot of glue grit, I could use this to smaller numbers in a slice. I don't need to release this like a pointer in C, right? Is there a way to later add/remove byte values to this? My more immediate use case would be to use this as a local memory efficient cache I can use to build matrix's with.

abstract struct ComplexN lice = Slice(UInt8).empty # imaginary i but it wouldn't be obvious so it is l kice = Slice(UInt8).empty # imaginary k jice = Slice(UInt8).empty # imaginary j end

I tried to do slices on variables in a class and I got a compiler error. I forgot which one, but I think it made sense, so that is why I extracted out the three variables to this abstract struct. I would be lying to say I know what I am doing, I am somewhat in that uncanny valley of the little I know and what I don't know. So take this entire post with a grain of salt.

- What aspect of the language would you like to see improved?

Since I am from a more dynamic language but wanting to work with games/embedded, I think what I would want is a convenient toolkit of functions for my edge case, but I admit I am a tad ignorant on the how, just pasted a little piece of code of something I did.

- What are the reasons?

Above, related to bindings for SDL, including more embedded devices with more constrained memory demands. Especially with older console bindings for SDL.

Include practical examples to illustrate your points.

In more constrained devices, I imagine I might be using bitwise operators doing shifting and comparisons of one slice to another. If you did say a 4-D projection of Quaternion numbers in a render. Something like the operators found here. Practical here would be I suppose memory comparisons and allocation on the lower-level. https://crystal-lang.org/reference/1.10/syntax_and_semantics/operators.html

Thank you for your time reading this rather abstract and vague post, but I do wish and hope for you all a happy holiday season!

yxhuvud commented 10 months ago

Quaternions probably belong in a shard given that they are quite rarely used. Aside from that I do not understand what this proposal is asking for. Please be more concrete in what you want to achieve and what you have tried, and also why you think the language isn't able to express what you want to express. I feel this issue should have fit better on the forum

That said I really wouldn't mind having implementations of Vec2(Number) and Vec3(Number) with some basic functionality to do math predefined in the stdlib. Those two are quite common (even outside AoC).

UltimaTerra commented 10 months ago

You are right, I should have asked the forum first, but I didn't also want a quaternion standard lib implementation. I just wanted a way to store small byte ints into a slice cache and apply methods on like read/write, as well as if there was a need to de-reference them like a pointer.

I tried using the .empty syntax inside class variables, but they give you an error because they are too big. Makes sense, it borrows from a reference type. So my want was to store ints and complex numbers in a byte slice, and maybe perform binary bitwise operations. But yes, my bad, I should have asked the forum first.

straight-shoota commented 10 months ago

Let's take this to the forum first. We can come back to an issue if there's any actionable item.