ChanServ changed the topic of #crystal-lang to: The Crystal programming language | | Fund Crystal's development: | GH: | Docs: | Gitter:
<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> <> a bit rough:
<FromGitter> <> 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> <> advance in the string?
<FromGitter> <> you could lchop/rchop
<FromGitter> <alexherbo2> ```code paste, see link``` []
<FromGitter> <alexherbo2> s/parser/scanner/ probably
<FromGitter> <alexherbo2> so it could handle chunks easily
<FromGitter> <> what's the diff?
<FromGitter> <> There is also Lexer
<FromGitter> <> scanner == lexer, sounds to be
<FromGitter> <> it is said in Wikipedia that the scanner is the first step of the lexer
<FromGitter> <> 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> <> Which vim plugis are people using for Crystal? I tried vim-crystal, but 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> <> 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> <> 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> <> js ( 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> <> well indentation would be nice :D
<FromGitter> <> but I now just added an ftdetect to set .cr to ruby and that works kinda well
<FromGitter> <> I think what I want is everything from ruby, but syntax highlighting from vim-crystal
<FromGitter> <> 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> <> 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 + + 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> <> is it recommended to use channels to manage sockets?
<FromGitter> <> can I select a channel and a socket?
<FromGitter> <> and use the channel to cancel?
<FromGitter> <Daniel-Worrall> In MT land, I would think so
<FromGitter> <> are we in MT land yet?
<FromGitter> <> 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> <> 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> <> so how would I do it in non-MT land?
<FromGitter> <> All I have is a fiber running receive in a loop, and I want to be able to stop it
<FromGitter> <> given this is a library, it should probably work without MT ;)
<FromGitter> <> when doing this in C, I'd have a socket and a pipe on which I call select() / epoll()
<FromGitter> <> and use the pipe to send a byte to cancel
<FromGitter> <Daniel-Worrall> It sounds fine, but I'd rather see code
<FromGitter> <> well, there is no code since I don't know how to do it in Crystal 😀
<FromGitter> <> 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> <> ```code paste, see link``` []
<FromGitter> <> and then from another fiber: `sock.close`
<FromGitter> <> this results in an `IO::Error`
<FromGitter> <> so, extremely generic
<FromGitter> <Daniel-Worrall> you may have to use Channels
<FromGitter> <> 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> <> if I could get the errno from the IO::Error, that would be perfect
<FromGitter> <> 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> <> ```LibC.recvfrom(fd,*), slice.size, 0, sockaddr, pointerof(addrlen))```
<FromGitter> <> How is errno handled there? I do not see any errno handling there
<FromGitter> <> So I get `#<IO::Error:Closed stream>`
<FromGitter> <> but `#os_error` on it is nil
<FromGitter> <> ohh, I think it got it
<FromGitter> <> ```code paste, see link``` []
<FromGitter> <> this seems to work
<FromGitter> <> > 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> <> for a method it's possible that currently 0 types exist that, if passed, will make it compilable
<FromGitter> <> but it doesnt mean that such a type can't be created
<FromGitter> <> 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> <> @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> <> is there like an opposite of IO::Memory?
<FromGitter> <> 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
<FromGitter> <Blacksmoke16> `Memory` part just means its stored in memory
<FromGitter> <> ah, right
<FromGitter> <> and then instead of write_bytes read_bytes
<FromGitter> <> cool, thx
<straight-shoota> exactly
<FromGitter> <> and I guess if I read more than is there, it raises an exception?
<FromGitter> <> perfect!
<straight-shoota> you can even make it writable, if you want :D
<FromGitter> <> oh god no 😀
<straight-shoota> then you should actually pass `writable: false`
<FromGitter> <> will that overwrite from the start or append, though? 🤔
<FromGitter> <> `writeable`, apparently
<FromGitter> <> (with e)
<straight-shoota> yeah
postmodern has joined #crystal-lang
<FromGitter> <> does crystal have integer promotion rules like C?
<FromGitter> <> lets say I do `id = packet[0] << 8 | packet[1]` and packet is Bytes
<FromGitter> <> and I want a UInt16 - will that works?
<straight-shoota> no because:
<straight-shoota> >> 1_u8 << 8
<FromGitter> <> so packet[0].to_u16 then?
<FromGitter> <> `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> <> yeah I started out with that
<FromGitter> <> but then noticed I also need random access
<straight-shoota> with IO::Memory you can set pos = x
<FromGitter> <> my idea was to handle the first 12 bytes like this, and then get an[12, packet.size - 12], writeable: false)
<FromGitter> <> oh actually, I only need the first 4
<FromGitter> <> so I can already convert that to IO
<FromGitter> <> straight-shoota:,215
<straight-shoota> yeah should just use IO::Memory from the beginning
<FromGitter> <> and then store pos, read, reset pos?
<FromGitter> <> 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> <> ohhh, nice
<FromGitter> <> thx!
<FromGitter> <> straight-shoota: Yeah, this is better:,215
<straight-shoota> great
<straight-shoota> IO::ByteFormat::NetworkEndian would prob be more appropriate (although there's no actual difference)
<FromGitter> <> seems pointless to have two things with the same value? 🙂
<straight-shoota> kindof
<straight-shoota> semantic nitpicking
<straight-shoota> btw the code on is really hard to read... to much contrast, no highlights
<straight-shoota> is that a fossil default theme?
duane has joined #crystal-lang
<FromGitter> <> 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