ChanServ changed the topic of #crystal-lang to: The Crystal programming language | https://crystal-lang.org | 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> <alexherbo2> it's not easy
<FromGitter> <alexherbo2> `string.each_line` assumes the chunk are single line, and that they are chunks, but we don't know, could as well be the slurped json
<FromGitter> <jrei:matrix.org> a bit rough: https://carc.in/#/r/ai7x
<FromGitter> <jrei:matrix.org> you could check if the string starts with `[[` to avoid the exception
<FromGitter> <alexherbo2> do you think it is out of the scope of the crystal language to have a json parser which advance in the string?
<FromGitter> <jrei:matrix.org> advance in the string?
<FromGitter> <jrei:matrix.org> you could lchop/rchop
<FromGitter> <alexherbo2> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=6042ca7e5d0bfb4e588d0b56]
<FromGitter> <alexherbo2> s/parser/scanner/ probably
<FromGitter> <alexherbo2> so it could handle chunks easily
<FromGitter> <jrei:matrix.org> what's the diff?
<FromGitter> <jrei:matrix.org> There is also Lexer
<FromGitter> <jrei:matrix.org> scanner == lexer, sounds to be
<FromGitter> <jrei:matrix.org> it is said in Wikipedia that the scanner is the first step of the lexer
<FromGitter> <jrei:matrix.org> anyway, you can use the parser directly, yes
fifr has quit [Ping timeout: 256 seconds]
<FromGitter> <alexherbo2> with a `parser` I doesn't expect the parse to advance in the string and having `parser.parse(something)` the same result, while with a scanner I know it will advance
fifr has joined #crystal-lang
teardown has quit [Ping timeout: 268 seconds]
teardown has joined #crystal-lang
<FromGitter> <js:nil.im> Which vim plugis are people using for Crystal? I tried vim-crystal, but https://github.com/vim-crystal/vim-crystal/blob/master/indent/crystal.vim is suuuper buggy, always making a new line start at column 0 and realigning it back to column 0 as soon as you type `.` - basically making it worse than not having it at all
<FromGitter> <Blacksmoke16> One for sublime
<FromGitter> <js:nil.im> I don't want a proprietary editor. And I neither want one based on Electron 😉
<FromGitter> <Blacksmoke16> Oh sorry, you literally said "vim plugins" 🙈
<FromGitter> <js:nil.im> I'm tempted to just treat .cr files as .rb files
fifr has quit [Ping timeout: 245 seconds]
fifr has joined #crystal-lang
fifr has quit [Ping timeout: 265 seconds]
hightower3 has quit [Remote host closed the connection]
fifr has joined #crystal-lang
andremedeiros has quit [Read error: Connection reset by peer]
<FromGitter> <brad:synapse.resuni.com> js (https://matrix.to/#/@js:nil.im): Personally, I just took the syntax highlighting from vim-crystal. I assume you want more than that though lol
andremedeiros has joined #crystal-lang
<FromGitter> <js:nil.im> well indentation would be nice :D
<FromGitter> <js:nil.im> but I now just added an ftdetect to set .cr to ruby and that works kinda well
<FromGitter> <js:nil.im> I think what I want is everything from ruby, but syntax highlighting from vim-crystal
<FromGitter> <brad:synapse.resuni.com> That makes sense, unless there's some kind of special spacing you want after type declarations or something.
_ht has joined #crystal-lang
hendursa1 has joined #crystal-lang
hendursa1 has quit [Remote host closed the connection]
hendursaga has quit [Ping timeout: 268 seconds]
hendursa1 has joined #crystal-lang
_whitelogger has joined #crystal-lang
postmodern has quit [Quit: Leaving]
hendursa1 has quit [Quit: hendursa1]
hendursaga has joined #crystal-lang
<hendursaga> Quick question: are conditionals in macros determined at compile-time?
<straight-shoota> In macro expressions, yes
<hendursaga> What kind of limitations are there?
<straight-shoota> that's the point of macros
<hendursaga> say that I wanted some code in a frequently called method only execute if, say, the object it is in has some variable set - is that possible?
<hendursaga> so that, an if-statement that is defined in the macro is not called thousands of times unnecessarily
<FromGitter> <Blacksmoke16> Would be possible via a constant or env car. Otherwise macros don't have access to runtime variables
<FromGitter> <Blacksmoke16> env car*
<FromGitter> <Blacksmoke16> lol stupid autocorrect
<FromGitter> <Blacksmoke16> Or build time flag
<hendursaga> hmmm, so like, a object is instantiated with something that can be known at compile-time - macros can use that?
<FromGitter> <Blacksmoke16> but it sounds like a macro isn't going to help in the case if you need the conditional logic dependent on a runtime value
<FromGitter> <Blacksmoke16> but yes, it would have to have something that is available at compile time. Like a constant, env var, annotation, or build time flag
<hendursaga> hmmm, cool, I'll need some time to wrap my head around it all lol
<hendursaga> there is no crazy borrow checker like in Rust right?
<FromGitter> <Blacksmoke16> do you have some example code of what you're trying to do?
<FromGitter> <Blacksmoke16> i dont know rust, but im going to go with no? :p
<hendursaga> not yet, no, I'm still thinking things through in my head
<FromGitter> <Blacksmoke16> 👍
<FromGitter> <Daniel-Worrall> No, it doesn't have a borrow checker
<FromGitter> <Daniel-Worrall> Rust is unique and the only language that has that afaik
<FromGitter> <Daniel-Worrall> Maybe you don't have code, do you have a use-case?
<hendursaga> yeah someday I'll get into it, but it's too much a pain to learn
<FromGitter> <Daniel-Worrall> There's a barrier of entry, for sure.
<FromGitter> <Daniel-Worrall> It's one of my goals to get proficient in
<hendursaga> basically I'm trying to port an old Ruby project of mine, a chess variants engine, and learn from my mistakes so the code isn't as unmanageable
<FromGitter> <Blacksmoke16> step 1, write unit tests :p
<FromGitter> <js:nil.im> So, how can I cleanly shut down a socket from another fiber that is currently in receive()?
<hendursaga> Blacksmoke16: ha, well, I did have integration tests, if that counts for something :P
<FromGitter> <Blacksmoke16> that works for me ha
<FromGitter> <Daniel-Worrall> Unit tests with TDD is the way to go, but it's a task to learn that methodology
<FromGitter> <Blacksmoke16> esp in crystal land
<hendursaga> but for instance, the vast vast majority of variants have a square board, some have a rectangular board, and others have weird stuff like citadels to consider
<FromGitter> <Blacksmoke16> is possible there are bugs but since the compiler removes unused methods/types, those bugs wouldnt surface if you didnt have *some* code using those methods/types
<FromGitter> <Daniel-Worrall> Are those variants known at compile time? If so, it can be a compile time macro conditional
<hendursaga> I'm pretty sure they would be, yeah
<hendursaga> so like, I was thinking of encoding the board as a simple, flat array, and some helper functions to convert from "normal" coordinates to the array coordinates..
<FromGitter> <Daniel-Worrall> Yeah, I'd love to see a flag introduced that could test unused methods
<FromGitter> <Daniel-Worrall> or at least see if they are compilable
<hendursaga> so say a1 = 0, a2 = 1, b1 = 7, etc
<FromGitter> <Blacksmoke16> write tests for them, problem solved
<FromGitter> <Daniel-Worrall> That's problem moved, not solved :^)
<FromGitter> <Blacksmoke16> mm i say solved, prevents both compile and runtime bugs 😉
<FromGitter> <Daniel-Worrall> It'd also be nice to spit out any uncalled methods so you can write tests for them
<FromGitter> <Daniel-Worrall> The problem becomes writing the tests ;)
<hendursaga> so a bishop, say, for square boards would simply have moves like self.pos + self.game.board.size + 1 but that code wouldn't work with rectangular or other exotic boards
<FromGitter> <Daniel-Worrall> and if those methods call external services, sometimes it's not so simple to just call it because it'll interfere with things outside of the program
<FromGitter> <js:nil.im> is it recommended to use channels to manage sockets?
<FromGitter> <js:nil.im> can I select a channel and a socket?
<FromGitter> <js:nil.im> and use the channel to cancel?
<FromGitter> <Daniel-Worrall> In MT land, I would think so
<FromGitter> <js:nil.im> are we in MT land yet?
<FromGitter> <js:nil.im> I found something from 2018 where it was an experiment
<FromGitter> <Daniel-Worrall> behind a flag, yes
<FromGitter> <Blacksmoke16> its still in that state
<FromGitter> <js:nil.im> I see
<FromGitter> <Daniel-Worrall> It hasn't changed in a year or so, but it's stable if you are very careful with it
<FromGitter> <js:nil.im> so how would I do it in non-MT land?
<FromGitter> <js:nil.im> All I have is a fiber running receive in a loop, and I want to be able to stop it
<FromGitter> <js:nil.im> given this is a library, it should probably work without MT ;)
<FromGitter> <js:nil.im> when doing this in C, I'd have a socket and a pipe on which I call select() / epoll()
<FromGitter> <js:nil.im> and use the pipe to send a byte to cancel
<FromGitter> <Daniel-Worrall> It sounds fine, but I'd rather see code
<FromGitter> <js:nil.im> well, there is no code since I don't know how to do it in Crystal 😀
<FromGitter> <js:nil.im> Or did you mean you want to see C code of what I'm trying to do?
<FromGitter> <Daniel-Worrall> na I meant Crystal
<FromGitter> <Daniel-Worrall> just me not being able to put words into code and think about the workings
<FromGitter> <js:nil.im> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60439b46a3a2f04f1fd9da72]
<FromGitter> <js:nil.im> and then from another fiber: `sock.close`
<FromGitter> <js:nil.im> this results in an `IO::Error`
<FromGitter> <js:nil.im> so, extremely generic
<FromGitter> <Daniel-Worrall> you may have to use Channels
<FromGitter> <js:nil.im> I tried using `until sock.eof?` instead of `loop`, but that doesn't work either, since it will make the currently running receive fail
<FromGitter> <js:nil.im> if I could get the errno from the IO::Error, that would be perfect
<FromGitter> <js:nil.im> the error description says that it was closed, if I could get the same info programmatically, I could just ignore that error and be done
<FromGitter> <js:nil.im> ```LibC.recvfrom(fd, slice.to_unsafe.as(Void*), slice.size, 0, sockaddr, pointerof(addrlen))```
<FromGitter> <js:nil.im> How is errno handled there? I do not see any errno handling there
<FromGitter> <js:nil.im> So I get `#<IO::Error:Closed stream>`
<FromGitter> <js:nil.im> but `#os_error` on it is nil
<FromGitter> <js:nil.im> ohh, I think it got it
<FromGitter> <js:nil.im> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60439e80d1aee44e2dcaf16d]
<FromGitter> <js:nil.im> this seems to work
<FromGitter> <oprypin:matrix.org> > Yeah, I'd love to see a flag introduced that could test unused methods ⏎ > or at least see if they are compilable ⏎ @Daniel-Worrall it's not decidable
<FromGitter> <Daniel-Worrall> shame
<FromGitter> <oprypin:matrix.org> for a method it's possible that currently 0 types exist that, if passed, will make it compilable
<FromGitter> <oprypin:matrix.org> but it doesnt mean that such a type can't be created
<FromGitter> <oprypin:matrix.org> although... hmm.. what if you're fine with checking only among existing types. that'd just be expensive but probably possible
DTZUZU has quit [Read error: Connection reset by peer]
<FromGitter> <Daniel-Worrall> Check the types that match the restrictions. If no restriction, work from the most likely one to work?
<FromGitter> <Blacksmoke16> i had an idea, can you use a macro to generate doc comments? 🤔
alexherbo2 has joined #crystal-lang
hightower4 has joined #crystal-lang
alexherbo21 has joined #crystal-lang
alexherbo2 has quit [Read error: Connection reset by peer]
alexherbo21 is now known as alexherbo2
alexherbo24 has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 256 seconds]
alexherbo24 is now known as alexherbo2
alexherbo2 has quit [Ping timeout: 256 seconds]
alexherbo2 has joined #crystal-lang
<FromGitter> <oprypin:matrix.org> @Blacksmoke16: what's that idea *for*?
<FromGitter> <Blacksmoke16> I didn't get that far yet
alexherbo27 has joined #crystal-lang
alexherbo2 has quit [Read error: Connection reset by peer]
alexherbo27 is now known as alexherbo2
HumanG33k has joined #crystal-lang
HumanG33k has quit [Remote host closed the connection]
HumanG33k has joined #crystal-lang
alexherbo2 has quit [Quit: Ping timeout (120 seconds)]
alexherbo2 has joined #crystal-lang
alexherbo23 has joined #crystal-lang
alexherbo2 has quit [Read error: Connection reset by peer]
alexherbo23 is now known as alexherbo2
alexherbo26 has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 264 seconds]
alexherbo26 is now known as alexherbo2
_ht has quit [Remote host closed the connection]
<FromGitter> <js:nil.im> is there like an opposite of IO::Memory?
<FromGitter> <js:nil.im> I have memory but want to treat it as a stream, rather than write to a stream to set memory
<FromGitter> <Blacksmoke16> Wouldn't that just be IO
<straight-shoota> that's also IO::Memory
<straight-shoota> IO::Memory.new(slice_of_data)
<FromGitter> <Blacksmoke16> `Memory` part just means its stored in memory
<FromGitter> <js:nil.im> ah, right
<FromGitter> <js:nil.im> and then instead of write_bytes read_bytes
<FromGitter> <js:nil.im> cool, thx
<straight-shoota> exactly
<FromGitter> <js:nil.im> and I guess if I read more than is there, it raises an exception?
<FromGitter> <js:nil.im> perfect!
<straight-shoota> you can even make it writable, if you want :D
<FromGitter> <js:nil.im> oh god no 😀
<straight-shoota> then you should actually pass `writable: false`
<FromGitter> <js:nil.im> will that overwrite from the start or append, though? 🤔
<FromGitter> <js:nil.im> `writeable`, apparently
<FromGitter> <js:nil.im> (with e)
<straight-shoota> https://crystal-lang.org/api/0.36.1/IO/Memory.html#new(slice:Bytes,writeable=true)-class-method
<straight-shoota> yeah
postmodern has joined #crystal-lang
<FromGitter> <js:nil.im> does crystal have integer promotion rules like C?
<FromGitter> <js:nil.im> lets say I do `id = packet[0] << 8 | packet[1]` and packet is Bytes
<FromGitter> <js:nil.im> and I want a UInt16 - will that works?
<straight-shoota> no because:
<straight-shoota> >> 1_u8 << 8
<FromGitter> <js:nil.im> so packet[0].to_u16 then?
<FromGitter> <js:nil.im> `packet[0].to_u16 << 8 | packet[1]`, that is
<straight-shoota> hm, DeBot seems to be sleeping
<straight-shoota> There is actually a much better way to do this in Crystal
<straight-shoota> wrap packet in an IO::Memory and call read_bytes(UInt16)
<straight-shoota> the IO interface is really neat for reading binary data.
<straight-shoota> test: #10475
<FromGitter> <js:nil.im> yeah I started out with that
<FromGitter> <js:nil.im> but then noticed I also need random access
<straight-shoota> with IO::Memory you can set pos = x
<FromGitter> <js:nil.im> my idea was to handle the first 12 bytes like this, and then get an IO::Memory.new(packet[12, packet.size - 12], writeable: false)
<FromGitter> <js:nil.im> oh actually, I only need the first 4
<FromGitter> <js:nil.im> so I can already convert that to IO
<FromGitter> <js:nil.im> straight-shoota: https://fossil.nil.im/asyncdns-cr/file?name=src/resolver.cr&ci=e71ccf3e48&ln=146,215
<straight-shoota> yeah should just use IO::Memory from the beginning
<FromGitter> <js:nil.im> and then store pos, read, reset pos?
<FromGitter> <js:nil.im> oh wait I don't need to, only for reading the original query and comparing ;)
<straight-shoota> all reads are just sequential
<straight-shoota> btw. `context = @queries.fetch(id) { return }` avoids raising
<FromGitter> <js:nil.im> ohhh, nice
<FromGitter> <js:nil.im> thx!
<FromGitter> <js:nil.im> straight-shoota: Yeah, this is better: https://fossil.nil.im/asyncdns-cr/file?name=src/resolver.cr&ci=86ae1b8057&ln=146,215
<straight-shoota> great
<straight-shoota> IO::ByteFormat::NetworkEndian would prob be more appropriate (although there's no actual difference)
<FromGitter> <js:nil.im> seems pointless to have two things with the same value? 🙂
<straight-shoota> kindof
<straight-shoota> semantic nitpicking
<straight-shoota> btw the code on fossil.nil.im is really hard to read... to much contrast, no highlights
<straight-shoota> is that a fossil default theme?
duane has joined #crystal-lang
<FromGitter> <oprypin:matrix.org> https://www.fossil-scm.org/home/file?name=src/fileedit.c&ci=trunk is *the* default theme probably
<straight-shoota> that's definitely easier on the eyes
<straight-shoota> no highlighting tho
robertmeta has quit [Ping timeout: 240 seconds]
r0bby has quit [Ping timeout: 240 seconds]
r0bby has joined #crystal-lang
robertmeta has joined #crystal-lang