jhass changed the topic of #crystal-lang to: The Crystal programming language | https://crystal-lang.org | Crystal 0.35.1 | Fund Crystal's development: https://crystal-lang.org/sponsors | GH: https://github.com/crystal-lang/crystal | Docs: https://crystal-lang.org/docs | Gitter: https://gitter.im/crystal-lang/crystal
<FromGitter> <j8r> What's the common way to expose an immutable stdlib object, like Array or Hash?
<FromGitter> <j8r> I usually make my own methods, but that's a bit redundant...
<FromGitter> <j8r> The problem is using a getter exposes a reference, which can be modified... I may create a shard to have an "immutable" wrapper struct, or using another macro-fu solution
<FromGitter> <Blacksmoke16> i know some libs return a cloned instance
HumanG33k has quit [Remote host closed the connection]
<oprypin> j8r, write in docs "don't modify it"
HumanG33k has joined #crystal-lang
HumanG33k has quit [Remote host closed the connection]
HumanG33k has joined #crystal-lang
<FromGitter> <incognitorobito> Is it possible to alias LibC functions?
<FromGitter> <incognitorobito> Wait, nvm
zorp has joined #crystal-lang
f1reflyylmao has joined #crystal-lang
f1refly has quit [Ping timeout: 246 seconds]
avane has quit [Quit: ZNC - https://znc.in]
avane has joined #crystal-lang
<FromGitter> <HertzDevil> can you build crystal libraries that are consumed by other languages
<FromGitter> <HertzDevil> it seems crystal is doing the right thing if you define top-level `fun`s, but it can't hide crystal's main
_whitelogger has joined #crystal-lang
Liothen has quit [Ping timeout: 264 seconds]
Liothen has joined #crystal-lang
<FromGitter> <incognitorobito> I'm having some fun learning win32 APIs and return codes lol
<FromGitter> <mattrberry> How do I take the pointer of a pointer in crystal?
<FromGitter> <Blacksmoke16> ```ptr = pointerof(var) ⏎ ⏎ ptr_ptr = pointerof(ptr)``` [https://gitter.im/crystal-lang/crystal?at=5fa7811606fa0513dd9cd2dd]
<FromGitter> <Blacksmoke16> ?
<FromGitter> <mattrberry> `Error: can't take address of pointerof(source)`
<FromGitter> <Blacksmoke16> oof
<FromGitter> <mattrberry> When I do `pointerof(pointerof(source))`
<FromGitter> <Blacksmoke16> right, need to do it in two separate calls like i ahd
<FromGitter> <mattrberry> Ahh sorry, thanks
<FromGitter> <mattrberry> Sort of related: This function seems like it's taking a Pointer(Pointer(UInt8)), which is supposed to be the pointer to a pointer of a String. In crystal, when I get the pointer to the pointer of a string, I end up with Pointer(Pointer(String)), which makes sense.. Why do you think this function is expecting a Pointer(Pointer(UInt8))?
<FromGitter> <mattrberry> Looks like the gl docs define it as: ⏎ ⏎ > *string* ⏎ > Specifies an array of pointers to strings containing the source code to be loaded into the shader. [https://gitter.im/crystal-lang/crystal?at=5fa782b58a236947ba90fd4d]
<FromGitter> <Blacksmoke16> fwiw that link you provided had it as `Pointer(Pointer(Char))`
<FromGitter> <mattrberry> Char is aliased to UInt8 in that module
<FromGitter> <Blacksmoke16> right, which isnt a `String`
<FromGitter> <mattrberry> Right
<FromGitter> <Blacksmoke16> but yea, maybe the binding is wrong?
<FromGitter> <Blacksmoke16> or outdated
<FromGitter> <mattrberry> So I'm trying to figure out what it's actually asking for but I'm not sure how to represent that in crystal
<FromGitter> <mattrberry> Here's an example of using that OpenGL method in C https://stackoverflow.com/questions/22100408/what-is-the-meaning-of-the-parameters-to-glshadersource/22100410
<FromGitter> <Blacksmoke16> https://stackoverflow.com/a/22100410 maybe helpful?
<FromGitter> <Blacksmoke16> oh lol
<FromGitter> <Blacksmoke16> yea i dunno tbh, dont really know C
<FromGitter> <mattrberry> One example in the answers there is ⏎ ⏎ ```const GLchar *source = "my awesome shader"; ⏎ glShaderSource(myShader, 1, &source, NULL);``` [https://gitter.im/crystal-lang/crystal?at=5fa783e9bf955735eb67845b]
<FromGitter> <mattrberry> I guess I can just do this and hope that it's what I want... ⏎ ⏎ ```pointerof(source_ptr).as(Pointer(Pointer(UInt8)))``` [https://gitter.im/crystal-lang/crystal?at=5fa7843dc10273610af1aeb9]
<FromGitter> <mattrberry> Ah, the crystal reference talks about this a little bit. I'm going to use this and just hope for the best for now ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5fa784a6b4283c208a507972]
<FromGitter> <mattrberry> I don't really know C or OpenGL, so I guess I'll know if it works once it works :p
<FromGitter> <Blacksmoke16> :P gl
zorp has quit [Quit: {#`%${%&`+'${`%&NO CARRIER]
_whitelogger has joined #crystal-lang
melthelesbian has quit [Ping timeout: 240 seconds]
kevinsjoberg has quit [Ping timeout: 240 seconds]
r0bby has quit [Ping timeout: 264 seconds]
jetpack_joe has quit [Ping timeout: 260 seconds]
issyl0 has quit [Ping timeout: 260 seconds]
jetpack_joe has joined #crystal-lang
r0bby has joined #crystal-lang
melthelesbian has joined #crystal-lang
kevinsjoberg has joined #crystal-lang
issyl0 has joined #crystal-lang
<FromGitter> <pebauer68> How could I get also the type of the instance vars ? ⏎ def list_instance_vars : String ⏎ ⏎ ```res = {{ @type.instance_vars.map &.name.stringify }}.each { |elem| puts elem }.to_s``` ⏎ ... [https://gitter.im/crystal-lang/crystal?at=5fa7bb002a35440715113774]
<FromGitter> <asterite> Matthew: A crystal string can be turned into pointer(UInt8) by calling to_unsafe on it. If you need a pointer of that, you can assign that to_unsafe call to a variable x and then pass pointerof(x) to C
<FromGitter> <Sija> any1 knows is there an easy way to run GHA CI against crystal nightlies (as in Travis)?
JuanMiguel has joined #crystal-lang
_whitelogger has joined #crystal-lang
JuanMiguel has quit [Quit: This computer has gone to sleep]
f1reflyylmao is now known as f1refly
<FromGitter> <Sija> Sweet! Thanks @oprypin! 🎉
<FromGitter> <j8r> oprypin ok... obviously not ideal and error prone.
<oprypin> j8r, ?
<FromGitter> <j8r> response of ⏎ ⏎ > @j8r, write in docs "don't modify it"
Human_G33k has joined #crystal-lang
HumanG33k has quit [Read error: Connection reset by peer]
<oprypin> j8r, possibly relevant for inspiration https://crystal-lang.org/api/master/Enumerable.html#each_cons(count:Int,reuse=false,&)-instance-method
<FromGitter> <j8r> interesting
<FromGitter> <j8r> However, I don't think returning a copy is a perfect solution, because it can be mutated and induce the user in error
<FromGitter> <j8r> an immutable would be better in that sense, since there won't be any `[]=` or `#push`, etc
<FromGitter> <j8r> and of course, there is a performance penalty to create a new array :/
<FromGitter> <j8r> thanks
<FromGitter> <j8r> It is doable to create a new type, and iterate over methods of Array an reject those inducing a mutation
<FromGitter> <j8r> or, having a more generic Enumerable/Indexable wrapper type
<oprypin> j8r, just make it implement Indexable and thats it. should be a `struct`.
<FromGitter> <j8r> yep
<FromGitter> <j8r> there are some specific methods related to Array that won't be present, though. May not be a big deal, because there is `Indexable#to_a`
<FromGitter> <j8r> the same cannot be said for Hash
<oprypin> yea 😬
duane has joined #crystal-lang
<FromGitter> <HertzDevil> more the reason to have an associative container module distinct from `Indexable`
sagax has quit [Ping timeout: 240 seconds]
<oprypin> yea
<oprypin> seems liek a perfect post-1.0 feature request 😊
go|dfish has quit [Ping timeout: 246 seconds]
<Andriamanitra> is there a shorthand for "arr.map{|x| fun(x)}"? similarly as with methods you could write simply "arr.map(&.fun)"
<FromGitter> <j8r> no
<Andriamanitra> that's unfortunate :(
<FromGitter> <Blacksmoke16> there is actually
<FromGitter> <Blacksmoke16> `arr.map &->fun(String)` where `String` is the type of each array element
<Andriamanitra> nice, although i assume you can't give the function additional parameters?
<FromGitter> <Blacksmoke16> right
JuanMiguel has joined #crystal-lang
masterdonx2 has quit [Quit: ZNC 1.7.5 - https://znc.in]
MasterdonX has joined #crystal-lang
dom96 has quit [Ping timeout: 246 seconds]
dom96 has joined #crystal-lang
<FromGitter> <mattrberry> How do I null-terminate a string before passing it to c?
JuanMiguel has quit [Quit: This computer has gone to sleep]
<FromGitter> <mattrberry> I'm trying ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5fa84516b86f6407043bbddf]
<FromGitter> <mattrberry> Oh `+` isn't the operator I want, one sec
<FromGitter> <mattrberry> Okay I was hoping that this would do it, but no luck so far. `source` is the Crystal String ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5fa845e906fa0513dd9e87b1]
<oprypin> @mattrberry what is `source`
<oprypin> oh wait u just said it
<oprypin> mattrberry, literally all you need (going from the former example) is `source_ptr = source.to_unsafe`
<FromGitter> <mattrberry> Does String.to_unsafe null-terminate the string? @oprypin
<oprypin> mattrberry, yes
<FromGitter> <mattrberry> Or does it just take the pointer?
<FromGitter> <mattrberry> Interesting, so I guess that's not my problem..
<oprypin> well yes it does just take the pointer, but string is guaranteed to have that null pointer at the end of the memory even though it's not necessary for its operation
<oprypin> s/null pointer/null byte/
<FromGitter> <mattrberry> Oh I didn't realize that Crystal strings were guaranteed to be null-terminated
<oprypin> mattrberry, also im not sure u may have some misunderstanding between `pointerof( )` and `to_unsafe`
<oprypin> so a Crystal type might happen to have some internal data that's useful to be given to C, then that type can have a `to_unsafe` method, which could be the only way to access that internal data
<oprypin> meanwhile, `pointerof( )` has nothing to do with that, it's quite simply a pointer *to* (on top of) the Crystal type
<FromGitter> <mattrberry> Crystal Strings don't define to_unsafe though,
<oprypin> oh they do
<FromGitter> <mattrberry> Sorry yeah I see that haha
<FromGitter> <mattrberry> @oprypin You have some experience with opengl, right?
<oprypin> minimal
<FromGitter> <mattrberry> That's specifically what I'm trying to use this for right now
<FromGitter> <mattrberry> I'm trying to pass a shader into glShaderSource
<oprypin> still not using sfml (which makes that trivial)?
<FromGitter> <mattrberry> sfml makes other stuff harder
<FromGitter> <mattrberry> It's not strictly an improvement
<oprypin> sorry for my plugging lol. well anyway i think i can help if you show me what you have on Crystal side and also the docs of the opengl function
<oprypin> mattrberry, so have u noticed that glShaderSource expects an array of strings? i don't understand, why is it an *array* what is the meaning behind string 1, string 2, string 3, ...?
<FromGitter> <mattrberry> Here's the link describing glShaderSource: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glShaderSource.xhtml ⏎ ⏎ Here's roughly what I'm doing, where `source` is the shader file read as a string: ⏎ ⏎ ```code paste, see link``` ... [https://gitter.im/crystal-lang/crystal?at=5fa849c8d37a1a13d6947227]
<oprypin> in any case let's assume you do have an array of strings in crystal: a = ["a", "b", "c"]
<FromGitter> <mattrberry> This answer seems to imply that you can just give it the pointer to a null-terminated string https://stackoverflow.com/a/22100409
<oprypin> ... then the call is `glShaderSource(handle, a.size, a.map(&.to_unsafe), a.map(&.size))`
<oprypin> (for the last two, an implicit `.to_unsafe` is added by crystal)
<oprypin> "you can just give it the pointer to a null-terminated string" - that, i guess, is true, but it still looks at it as an array of 1 null-terminated string
<oprypin> ok so you have 1 string after all. `s = "a"`
<FromGitter> <mattrberry> > *<oprypin>* ok so you have 1 string after all. `s = "a"` ⏎ ⏎ Hmm?
<oprypin> ... then the call is `sp = s.to_unsafe; glShaderSource(handle, 1, pointerof(sp), nil)`
<oprypin> ... alternatively `sp = s.to_unsafe; ss = s.size; glShaderSource(handle, 1, pointerof(sp), pointerof(ss))`
<FromGitter> <mattrberry> > *<oprypin>* ... alternatively `sp = s.to_unsafe; ss = s.size; glShaderSource(handle, 1, pointerof(sp), pointerof(ss))` ⏎ ⏎ Okay by doing this I don't get any errors from gl!
<FromGitter> <mattrberry> So the difference was calling `s.to_unsafe` rather than `pointerof(s)`
<FromGitter> <asterite> I think my previous message didn't go out
<FromGitter> <asterite> by calling to_unsafe on a string you get pointer(uint8), which is how a C "string" is represented
<FromGitter> <asterite> if you then assign that to a variable x and do `pointerof(x)` you get `pointer(pointer(uint8))` which is what you need for the C function you have there
<FromGitter> <mattrberry> Yep that's exactly what my problem was and is working now, thanks :)
duane has quit [Ping timeout: 260 seconds]
<yxhuvud> AHA! Just found the root of the issue I've been fighting with for several sessions. The tie-in to libevent is so hard I understand why the people that tried to do windows io want a rewrite of the event loop.
duane has joined #crystal-lang
<FromGitter> <incognitorobito> @yxhuvud Are you working on getting the event loop working on windows?
<yxhuvud> No. I'm working on monketpatching the normal libevent based event loop to instead use io_uring.
<yxhuvud> but a lot of the problems you run into is the same.
duane has quit [Ping timeout: 260 seconds]
<FromGitter> <j8r> yxhuvud you'll submit a PR/patch to libevent?!
<FromGitter> <j8r> I'm curious about what gain can yield io_uring, I doubt much
<FromGitter> <j8r> it could be significant on some workloads
Liothen has quit [Quit: Liothen]
yukai has joined #crystal-lang
early` has joined #crystal-lang
early has quit [Ping timeout: 260 seconds]
duane has joined #crystal-lang
<FromGitter> <mattrberry> Yay, shaders :)
<FromGitter> <Blacksmoke16> nice one
<FromGitter> <mattrberry> It's just color correction to more accurately reflect what the colors were meant to look like on hardware
<FromGitter> <mattrberry> Since colors get blown out on modern displays
<FromGitter> <mattrberry> Without color correction ^
<FromGitter> <Blacksmoke16> deff quite a bit brighter