ChanServ changed the topic of #crystal-lang to: The Crystal programming language | http://crystal-lang.org | Crystal 0.30.1 | Fund Crystal's development: http://is.gd/X7PRtI | GH: https://github.com/crystal-lang/crystal | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/ | Gitter: https://gitter.im/crystal-lang/crystal
gangstacat has quit [Quit: Ĝis!]
gangstacat has joined #crystal-lang
<FromGitter> <watzon> What's the easiest way to check if a file is the file being executed?
<FromGitter> <watzon> Since crystal doesn't have a `main` method
<FromGitter> <tenebrousedge> isn't there a `__FILE__` variable?
<FromGitter> <Blacksmoke16> What would that do?
<FromGitter> <watzon> Yeah which I suppose I could use in conjunction with ARGV, just wondering if there's an easier way
<FromGitter> <tenebrousedge> `if current_file == *FILE*` ??
duane has joined #crystal-lang
<FromGitter> <tenebrousedge> what exactly are you after?
<FromGitter> <watzon> A crystal equivalent of Python's `if *name* == '__main__':`
<FromGitter> <tenebrousedge> `if PROGRAM_NAME == *FILE*`
<FromGitter> <watzon> Doesn't work that way
<FromGitter> <watzon> Program name ends up looking like this `"/home/watzon/.cache/crystal/crystal-run-ngrok_webhook.tmp"`
<FromGitter> <tenebrousedge> not if you build instead of run
<FromGitter> <watzon> But that doesn't fit my usecase
<FromGitter> <tenebrousedge> what is your usecase?
<FromGitter> <watzon> In Ruby you can do ⏎ ⏎ ```if __FILE__ == $0 ⏎ foo() ⏎ bar() ⏎ end``` ⏎ ⏎ but this doesn't work in Crystal [https://gitter.im/crystal-lang/crystal?at=5d731849c38d634123ad7889]
<FromGitter> <tenebrousedge> ruby isn't compiled; in Crystal the compiled binary is not going to match the executed filename
<FromGitter> <watzon> I need an example file to be runnable via `crystal run`, but I also need certain parts to only run if the file itself is being called
<FromGitter> <watzon> Basically I need a block of code to not be compiled unless the file itself is the file being compiled
<FromGitter> <Blacksmoke16> can you use flags
<FromGitter> <watzon> Is there a flag that fits that usecase?
<FromGitter> <Blacksmoke16> i mean you can define your own
<FromGitter> <Blacksmoke16> prob less than ideal
<FromGitter> <tenebrousedge> `crystal run` compiles the file, though, right?
<FromGitter> <watzon> Yes
<FromGitter> <watzon> What I'm trying to do though is import a class from one example into another example
<FromGitter> <watzon> But all the examples also execute a code block, which I want to avoid unless the file itself is the one being compiled
<FromGitter> <watzon> Dumb haha
<FromGitter> <watzon> Oh well, it's not a huge deal
<FromGitter> <watzon> I just wanted to avoid duplicate code
<FromGitter> <tenebrousedge> it seems like that just sets a constant for the first file that runs?
<FromGitter> <tenebrousedge> which might be a good solution?
<FromGitter> <watzon> Except it didn't get merged
<FromGitter> <tenebrousedge> but you can do something similar? It looks like just one line of code
<FromGitter> <watzon> No, to get it to work I'd have to patch `src/compiler/crystal/program.cr` like he did here https://github.com/crystal-lang/crystal/pull/7466/files
<FromGitter> <watzon> And then do what he used as an example
<FromGitter> <Blacksmoke16> https://crystal-lang.org/api/master/Crystal.html#main(&block)-class-method
<FromGitter> <watzon> It's alright though, I'll just deal with a little bit of duplicate code
<FromGitter> <watzon> Interesting @Blacksmoke16, I wonder if that would actually work here
<FromGitter> <tenebrousedge> defining a constant if not already defined shouldn't require patching the compiler
<FromGitter> <watzon> It does in this case. The compiler has to be modified to make a constant like that possible.
<FromGitter> <watzon> It's basically a magic constant
<FromGitter> <tenebrousedge> I don't see how, but you do you
<FromGitter> <watzon> This (https://github.com/crystal-lang/crystal/pull/7466/files#diff-7677190dbcbab1193561f41041384764L111) is the property I'd need, but it's not currently exported by the compiler. And even if it was, there's no way to access the compiler inside of the build context. The compiler has to create and export the constant.
<FromGitter> <tenebrousedge> if you're convinced there's no way to make that work, I will believe you
<FromGitter> <tenebrousedge> it certainly seems like it would be required to patch the compiler if you didn't want to test whether the constant was already defined in every file
_whitelogger has joined #crystal-lang
chemist69 has quit [Ping timeout: 252 seconds]
duane has quit [Ping timeout: 246 seconds]
chemist69 has joined #crystal-lang
_whitelogger has joined #crystal-lang
_whitelogger has joined #crystal-lang
hightower3 has quit [Ping timeout: 245 seconds]
_whitelogger has joined #crystal-lang
<FromGitter> <bararchy> @bcardiff I mean `snaps install crystal` etc.. inside a docker won't work, it needs al kind of hacks to work (it needs a daemon running, etc..)
chachasmooth_ has joined #crystal-lang
chachasmooth has quit [Ping timeout: 250 seconds]
ht_ has joined #crystal-lang
alex``` has joined #crystal-lang
blassin has joined #crystal-lang
ht_ has quit [Quit: ht_]
hightower3 has joined #crystal-lang
DTZUZO has quit [Ping timeout: 245 seconds]
alex``` has quit [Ping timeout: 245 seconds]
alex``` has joined #crystal-lang
alex``` has quit [Ping timeout: 244 seconds]
alex``` has joined #crystal-lang
<FromGitter> <j8r> Snap kinda sucks for this
<FromGitter> <j8r> appimage as far as I know doesn't need a runtime, it's optional
<FromGitter> <j8r> Good article about how Appimage, Flatpack and Snap compare themselves https://www.ostechnix.com/linux-package-managers-compared-appimage-vs-snap-vs-flatpak/
<FromGitter> <Blacksmoke16> got ci running ameba, formatter and latest/nightly specs
<FromGitter> <Blacksmoke16> that runs on all non master branches
<FromGitter> <Blacksmoke16> then deploys api docs when merged into master
<FromGitter> <Blacksmoke16> kinda dont like it fails the checks when nightly fails, but is good to know something breaking is coming
<FromGitter> <Blacksmoke16> didnt make it required so is np if it fails when doing a pr
DTZUZO has joined #crystal-lang
sorcus has quit [Ping timeout: 264 seconds]
alex``` has quit [Ping timeout: 245 seconds]
alex``` has joined #crystal-lang
companion_cube has joined #crystal-lang
<companion_cube> hello, I'm interested in crystal as a kind of clean, typed scripting language (I hate python more and more). Seems like a lot of interesting ideas.
<FromGitter> <tenebrousedge> what do you need in a scripting language?
<companion_cube> mostly, unix stuff: file manip, subprocesses, this kind of stuff. `spawn` looks pretty good for that.
<companion_cube> (also, good tooling, mypy is … not perfect)
sorcus has joined #crystal-lang
<FromGitter> <Blacksmoke16> Easiest way would be to just give it a go and try it out
<companion_cube> yep :)
<FromGitter> <tenebrousedge> icr is pretty workable as a REPL, but not perfect. `crystal play` is a really nice command
<companion_cube> I just discovered that indeed
<companion_cube> does the vim extension automatically install the code analyser? seems like goto-def works
<FromGitter> <Blacksmoke16> No idea
<companion_cube> unrelatedly, is there a roadmap to 1.0, or is it in the distant future?
<companion_cube> ah, no, specific tool for `implementations`.
<FromGitter> <tenebrousedge> there is a roadmap of sorts, but 1.0 is probably a "distant future"
<FromGitter> <Blacksmoke16> main things are MT and windows
<companion_cube> so, there could be arbitrary breaking changes, for years, right?
<companion_cube> what about the GC? boehm doesn't seem very exciting
<FromGitter> <Blacksmoke16> mm from what i read its not a big priority atm, https://github.com/crystal-lang/crystal/issues/5271#issuecomment-343463153
<companion_cube> I'm somewhat surprised it didn't go the refcounting way
<FromGitter> <Blacksmoke16> :shrug: not my area of expertise :p
<companion_cube> is there a "zen" of crystal? :)
<companion_cube> or a kind of list of design goals
<companion_cube> what do you use it for?
<FromGitter> <Blacksmoke16> https://github.com/crystal-lang/crystal#crystal readme has most of that stuff
<companion_cube> thanks :)
<FromGitter> <Blacksmoke16> most web based api stuff
<FromGitter> <Blacksmoke16> mostly*
<companion_cube> feels like it can position itself as a competitor to Go
<FromGitter> <tenebrousedge> Crystal inherits a lot from Ruby, and I would describe both as being concise, Unixy, and favoring convenient syntax
<FromGitter> <Blacksmoke16> have to wait and see 😉
<companion_cube> well, lightweight spawn, static typing, value types… could be as fast :)
<FromGitter> <tenebrousedge> speed of execution isn't necessarily an area where I would expect Crystal to beat Go
<FromGitter> <tenebrousedge> Crystal aims to be quick to develop
<companion_cube> maybe not beat, but be comparable, I imagine. the distinction `struct`/`class` suggests to me that speed still matters :)
<FromGitter> <Blacksmoke16> can always optimize later
<FromGitter> <tenebrousedge> that's the first rule of optimization :)
<FromGitter> <Blacksmoke16> even not focusing 100% on performance you can still make pretty performant stuff
<companion_cube> yep
<companion_cube> I mean, the language matters; with python it's quite hard to optimise to the point where it's competitive with Go :p
<companion_cube> anyway, I also hope for better tooling, can't live without completion and type checking these days
<FromGitter> <Blacksmoke16> indeed
<companion_cube> also seems like the stdlib is huge!!
<FromGitter> <Blacksmoke16> some would say *too* big
<companion_cube> ah, well, the eternal question.
<FromGitter> <tenebrousedge> well >_> there are some reasonably strong arguments (https://github.com/crystal-lang/crystal/issues/5215)
<FromGitter> <tenebrousedge> reasonably strong arguments
<FromGitter> <tenebrousedge> weird how links work differently for issues
<FromGitter> <vlazar> how do I write from `Slice` to smaller `Slice`? ⏎ ⏎ I'm using `rest.copy_from(chunk.to_unsafe, last_bytes_count)`
<FromGitter> <vlazar> where `chunk` is like `4096` bytes and `rest` if for last piece of data I need to write to file
<FromGitter> <vlazar> I don't like to use `chunk.to_unsafe`
<FromGitter> <dscottboggs_gitlab> it looks like you're trying to copy from a smaller slice to a bigger one, not the other way around... when dealing with slices, you need to ensure `rest` is large enough to hold the copied data before performing the copy
<FromGitter> <dscottboggs_gitlab> the requirement to call `#to_unsafe` is a reminder that that action *is* unsafe, and you need to check to make sure you can do it before you do it
<FromGitter> <dscottboggs_gitlab> I would do something like ⏎ ⏎ ```Slice.new``` [https://gitter.im/crystal-lang/crystal?at=5d73d44750508949d3f33fcc]
<FromGitter> <dscottboggs_gitlab> shit
<FromGitter> <dscottboggs_gitlab> I would do something like ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5d73d4a4b9abfa4bd3688f3a]
<FromGitter> <dscottboggs_gitlab> no that should be indexing not adding
HumanGeek has joined #crystal-lang
<FromGitter> <dscottboggs_gitlab> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5d73d7733b1e5e5df16cb220]
Human_G33k has quit [Ping timeout: 245 seconds]
<FromGitter> <dscottboggs_gitlab> It'd be nice if there were a standardized way to move rest into the new slice though :/
<FromGitter> <SewerynKaminski> I have some strange results in my benchmark. I test 3 functions one regular, one optimised and one optimised+. The first two plays nicely times are always the same regular one are slow and optmised are fast. But optimised+ give incredible short times or simply short times when I put 'puts' call before benchmark. Are you interested in this? The code is very short and free :)
<FromGitter> <kingsleyh> hello - I've written this today - it's still very experimental and needs lots of work - but conceptually - it seems to work quite well: https://github.com/SushiChain/spinach
<FromGitter> <tenebrousedge> @kingsleyh it's written in html?
<FromGitter> <kingsleyh> yeah you write the test in html
<FromGitter> <tenebrousedge> that seems strange to me
<FromGitter> <kingsleyh> it's BDD - which generally has specs written in html, markdown, or plain text
<FromGitter> <kingsleyh> but I like html - as it's easier to add images and supporting documentation etc
<FromGitter> <kingsleyh> the idea is you write a document that explains how your system works - and that document is executable
<FromGitter> <tenebrousedge> the BDD-style testing frameworks I've seen have used a domain-specific language
<FromGitter> <kingsleyh> yeah some like Cucumber have a DSL
<FromGitter> <kingsleyh> my library is closes to this one: https://concordion.org/
<FromGitter> <kingsleyh> you don't have to write BDD style of course using my thing - you can just write a document and execute parts of it
<oprypin> so i'm looking into github actions and this is nice and all https://github.com/actions/starter-workflows/blob/master/ci/crystal.yml
<oprypin> but how does one make this run also on macOS? it just says "##[error]File not found: 'docker'" :D
<FromGitter> <dscottboggs_gitlab> probs need to install docker :p
<oprypin> well at least it exists
<oprypin> now just need some non-ugly way for an OS-dependent install procedure; i.e. skip "container:" on mac
<FromGitter> <vlazar> to make my question about avoiding `.to_unsafe` on `Slice` here is an example ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5d73e432c5939027202d40f0]
<FromGitter> <vlazar> I'm want to read in chunks to use little memory and watch progress to show download indicator
<oprypin> vlazar, cant u do `chunk.copy_to(rest)` instead
<FromGitter> <vlazar> That's what I've started from actually when I read in the docs it should truncate
<FromGitter> <vlazar> but the it turned out it was old docs for Crystal 0.20 :S
<FromGitter> <dscottboggs_gitlab> @vlazar this is a good way to do it, the only improvement I can suggest (which would also remove the `#to_unsafe` call) is to remove the zero-initialization of the new slice by using the block version of `Slice.new`, I.E. ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5d73e4f750508949d3f3b80a]
<FromGitter> <dscottboggs_gitlab> this initializes the slice with values from `chunk` rather than zeroing it so it will be faster
<FromGitter> <dscottboggs_gitlab> `(chunk + idx).value` is the same as saying `*(ptr + offset)` in `C` if you're familiar
<FromGitter> <dscottboggs_gitlab> or you could just do ⏎ ⏎ ```file.write chunk[0..count]``` ⏎ ⏎ that'll work. [https://gitter.im/crystal-lang/crystal?at=5d73e590a08e2b4bd2a504e3]
DTZUZO has quit [Quit: WeeChat 2.0]
<FromGitter> <vlazar> Ah, thanks @dscottboggs_gitlab
<FromGitter> <vlazar> I somewhat forgot Crystal is close to Ruby
<FromGitter> <dscottboggs_gitlab> np :)
<FromGitter> <vlazar> Should have tried obvious choice `chunk[0..count]`
<FromGitter> <vlazar> only it should be `chunk[0...count]`
<FromGitter> <dscottboggs_gitlab> it's alright it took me a few iterations to get there too :)
<FromGitter> <dscottboggs_gitlab> oh ok
<FromGitter> <vlazar> will read on block initialization for Slice, interesting stuff
<FromGitter> <SewerynKaminski> Ex.1 ⏎ ⏎ ``` find string 3.81M (262.52ns) (± 5.40%) 385B/op 6.29× slower``` ⏎ ⏎ find string slice 15.30M ( 65.38ns) (± 0.13%) 0.0B/op 1.57× slower ... [https://gitter.im/crystal-lang/crystal?at=5d73e807be4a3f13fd0523b6]
<FromGitter> <vlazar> `slice[start..end]` seems to be zero copy ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5d73ed0fae44a84124998418]
dannyAAM has quit [Quit: znc.saru.moe : ZNC 1.6.2 - http://znc.in]
dannyAAM has joined #crystal-lang
<oprypin> oh yea that's not a copy
DTZUZO has joined #crystal-lang
<companion_cube> I like the distinction Iterable(T) vs Enumerable(T)
<companion_cube> I haven't seen anything for type aliases, though
<companion_cube> (if I want to name a union type nicely?)
<FromGitter> <Blacksmoke16> `alias MyType = String | Int32 | Foo`
<companion_cube> heh, simple indeed
<companion_cube> ah, no recursion though.
<FromGitter> <Blacksmoke16> `MyType = Int32 | String | Hash(String, MyType)`
<companion_cube> wondering if there's a natural encoding of sum types, but seems like the main AST uses inheritance (as in scala, in a way)
<FromGitter> <Blacksmoke16> that should work
<FromGitter> <watzon> @companion_cube, I'm sorry for what I did to you in Portal
<FromGitter> <watzon> GladOS gave me no choice
<companion_cube> you're forgiven ;)
<companion_cube> it burns though :o
<FromGitter> <dscottboggs_gitlab> @SewerynKaminski you were running into some optimizations. The "puts" call made it so that the values you were looking at were not optimized away.
HumanGeek has quit [Read error: Connection reset by peer]
<FromGitter> <dscottboggs_gitlab> Zero times for benchmarks come from something being precalculated at compile-time
<FromGitter> <SewerynKaminski> @dscottboggs_gitlab So, in debug mode this shouldn't happen? I have to check it
<FromGitter> <dscottboggs_gitlab> Not sure if that will prevent it, but you want to run benchmarks in release mode anyway.
<FromGitter> <dscottboggs_gitlab> best way I can think of to avoid that off of the top of my head (which works well since you're dealing with strings and slices) is to write them to /dev/null
HumanG33k has joined #crystal-lang
<FromGitter> <dscottboggs_gitlab> like ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5d7411953b1e5e5df16e51b8]
HumanG33k has quit [Remote host closed the connection]
<FromGitter> <dscottboggs_gitlab> idk there might be a better way though
HumanG33k has joined #crystal-lang
<FromGitter> <SewerynKaminski> In debug mode everithing looks good, its very slow. But consistent
gangstacat has quit [Quit: Ĝis!]
<FromGitter> <SewerynKaminski> @dscottboggs_gitlab Yes, thanks. It must be the optimalisation magic
<FromGitter> <dscottboggs_gitlab> np
<companion_cube> https://crystal-lang.org/reference/guides/concurrency.html oh jeez, the capture of `i` :(((
gangstacat has joined #crystal-lang
<companion_cube> it's also weird that IO isn't split into a reader and writer part
<companion_cube> (sorry for the stream of consciousness)
<FromGitter> <Blacksmoke16> you mean like https://crystal-lang.org/api/master/IO.html#pipe(read_blocking=false,write_blocking=false):Tuple(IO::FileDescriptor,IO::FileDescriptor)-class-method ?
<companion_cube> I mean, I read that to include the IO module one must define read and write
<FromGitter> <Blacksmoke16> right
<companion_cube> otherwise, the iterator-based design and all look super good for scripting/small programs ^^
<FromGitter> <Blacksmoke16> 👍
_whitelogger has joined #crystal-lang
<companion_cube> damn, the crirc library seems to fail when `ssl=true` is passed :(
<FromGitter> <Blacksmoke16> Hm?
<companion_cube> well, trying my favorite toy program, an IRC bot, and there's a lib for that but it seems to fail :/
chachasmooth_ has quit [Ping timeout: 264 seconds]
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
<FromGitter> <Blacksmoke16> Is it old?
<companion_cube> https://github.com/Meoowww/Crirc not that old, I imagine they just didn't test it too much with ssl on
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
<FromGitter> <Blacksmoke16> Some libs are quite out of date and no longer maintained
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
<FromGitter> <Blacksmoke16> Make a PR ;)
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
<companion_cube> well first I need to find out why :(
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
<FromGitter> <Blacksmoke16> Probably would be helpful
chachasmooth has joined #crystal-lang
chachasmooth has quit [Max SendQ exceeded]
<companion_cube> ok I think it's because the ssl wrapper bufferizes
<companion_cube> so messages are not sent
<companion_cube> let's PR that.
<companion_cube> exciting!
<FromGitter> <Blacksmoke16> Nice one
sorcus has quit [Ping timeout: 264 seconds]
alex``` has quit [Quit: WeeChat 2.5]
<companion_cube> hum, there isn't a method on regex to iterate over all the matches?
<FromGitter> <Blacksmoke16> im sure theres a way
<FromGitter> <Blacksmoke16> got some example code?
<companion_cube> I just want to count how many times a /foo/ occurs in a string
<companion_cube> with a word boundary, so probably like /\bfoo\b/
<FromGitter> <Blacksmoke16> cant just do like `str.match(/\bfoo\b/).size`?
<companion_cube> doesn't it return only the first match?w
<companion_cube> ohh, my bad
<companion_cube> ✔ thanks a lot
<FromGitter> <Blacksmoke16> np
<FromGitter> <Sija> /shameless plug/ for all of u `puts`/`pp`-style debugging fans, I’ve made https://github.com/Sija/debug.cr to make your life in terminal a bit moar colorful! give it a try :)
<FromGitter> <watzon> Ahh nice
<FromGitter> <watzon> I like it
<FromGitter> <watzon> Seeing as we're shamelessly plugging, I've been working on the wiki for my Tourmaline project. Pretty proud of it so far https://github.com/watzon/tourmaline/wiki
<FromGitter> <Blacksmoke16> how the annotation stuff go
<FromGitter> <watzon> Awesome. I'm super happy with it
<FromGitter> <watzon> Annotations allowed me to simplify a lot of things
<FromGitter> <watzon> And provide a nicer way to add bot commands
<FromGitter> <Blacksmoke16> good to hear
<FromGitter> <Sija> tourmaline looks pretty neat, cool stuff 👍
<FromGitter> <watzon> Thanks :)
<FromGitter> <Sija> ahhh, you’ve made ngrok shard too, that’s sweet man!
<FromGitter> <watzon> Yeah that was quite a while ago. It needs some updating to match what I've learned in Crystal over the past couple years, but it works :)
<FromGitter> <watzon> Makes developing bots a lot easier too
<FromGitter> <Sija> I bet it does :) especially with webhooks
<FromGitter> <watzon> Yep, that's my #1 usecase haha
<FromGitter> <watzon> It's why I made it in the first place
<FromGitter> <Sija> yeah, we use it extensively (ngrok) for the same purpose too at the place were I work atm
<FromGitter> <Sija> nothin’ works better with local development combined with 3rd party services using webhooks
<FromGitter> <Sija> btw, is ngrok.cr battle-tested? would it work if I’d to leave it for weeks/months to come?
<FromGitter> <watzon> It should, it just uses an installed version of ngrok in the background so it should be as reliable as ngrok itself
<companion_cube> hmmm MatchData.size is the number of captured elements, actually…
<FromGitter> <Blacksmoke16> can you add a type restriction for a specific module?
<FromGitter> <Blacksmoke16> im thinking no