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
alexherbo2 has quit [Remote host closed the connection]
deavmi has quit [Read error: Connection reset by peer]
deavmi has joined #crystal-lang
zorp has quit [Ping timeout: 272 seconds]
postmodern has joined #crystal-lang
f1reflyylmao has quit [Ping timeout: 240 seconds]
f1refly has joined #crystal-lang
<postmodern> how do you define an instance variable at the top-level? trying to use ivars in spectator specs for objects that need to be allocated at the beginning and closed at the end
<FromGitter> <Blacksmoke16> you cant
<FromGitter> <Blacksmoke16> would have to use a class var in a module
sagax has quit [Ping timeout: 260 seconds]
<postmodern> how do you escape #{{ macro_here }}?
<postmodern> \#{{ ... }} isn't doing the trick
<postmodern> (using macros to generate spectator spec descriptions)
<FromGitter> <Blacksmoke16> pretty sure you can just do like `"str {{macro_var}} bar"`
<postmodern> `describe "\#{{ name.id }}" do` is the problem line
<FromGitter> <Blacksmoke16> and whats the error?
<postmodern> it's not being interpolated. Spectator is printing out literal `"#{{name.id}}"`
<FromGitter> <Blacksmoke16> what happens if you get rid of the `\#`
<FromGitter> <Blacksmoke16> or do like `"\##{{{ name.id }}}"`
<postmodern> #{{name.id}} and ##{{name.id}}, respectively
<FromGitter> <Blacksmoke16> can you share some more code?
<FromGitter> <Blacksmoke16> pretty sure this works
<FromGitter> <Blacksmoke16> i think this is a bug imo, adding a space after the `#` fixes it
<FromGitter> <Blacksmoke16> oh
<FromGitter> <Blacksmoke16> nvm
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/9q27 :shrug:
<postmodern> interesting
<postmodern> hmm still getting ##{meta_output} for "\#{{{ name.id }}}", using crystal 0.35.1 and spectator 0.9.23
<FromGitter> <Blacksmoke16> Hm
<postmodern> im guessing spectator's macros are double evaluating the string or something?
<FromGitter> <Blacksmoke16> :shrug: never used it. Not worth the extra dependency/learning the new syntax
<postmodern> i'm used to rspec syntax, so more of a comfort thing
<FromGitter> <Blacksmoke16> Fair enough
<FromGitter> <Blacksmoke16> Granted I also a testing shard that uses a more unit test approach. But it just boils down into standard spec module stuff so :shrug:
<FromGitter> <Blacksmoke16> Have a*
<FromGitter> <Blacksmoke16> anyway, im off to bed o/
<FromGitter> <mwlang> trying to find a simple way to pluck a specific class out of an array of instantiated classes.... ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f62e590c1d1a53705a9a68e]
<FromGitter> <mwlang> PriceFilter is descendant of ExchangeFilter
<FromGitter> <mwlang> Ideally, would like to do something like this: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f62e624b468994d0d344640]
<FromGitter> <mattrberry> Hey all, I'm trying to do something a little fun with macros. I don't thing it's actually possible in crystal's macro system, but I just wanted to double check. I'm effectively trying to fill an array with function pointers, almost exactly like this (not my repo, but same idea): https://github.com/wheremyfoodat/Beeg-Advanced/blob/master/src/ARM/arm.rs#L26 ⏎ I'm already doing this at runtime, but I figured
<FromGitter> ... I'd experiment with the macro system just for fun. Is this possible?
<FromGitter> <naqvis> @mwlang `is_a?` expects a class, not an object or instance. you can use `typeof` in this case. like `is_a?(typeof(fc))` or `as(typeof(fc))`
<FromGitter> <naqvis> one possible way to achieve your desired outcome could be like ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f62fd3389b38d09213283b5]
<FromGitter> <naqvis> I would suggest to constraint the method arg to some specific parent type, so that compiler can reject calls which doesn't fall under specific hierarchy
<FromGitter> <naqvis> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f630086b468994d0d34838f]
<repo> mattrberry: i'd argue if you're using macros to generate methods, there's no need fill an array with it. You could just as well define them on a class or a module
<repo> if you _really_ want to have them in something you can later iterate over, you might want to store them in a tuple instead. This way you still have max type safety
<repo> but imho even then you should just define the methods on a class and just use a macro context to iterate over them (if you need to)
<repo> but to answer your question: sure it's possible
<repo> to give you an idea of what's possible with macros, have a look at this: https://p.jokke.space/m-KOb/ :D
<FromGitter> <Dan-Do> how do get value in spawn then assign to outer scope var?
<FromGitter> <Dan-Do> this works ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f631993603d0b37f4397854]
<FromGitter> <Dan-Do> this don't ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f6319b43651184d30f78a9f]
<FromGitter> <naqvis> Please use `Channel` to communicate
<FromGitter> <Dan-Do> Does it solve the issue that we have to sleep some seconds for the spawn run?
<FromGitter> <Dan-Do> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f631d0b3651184d30f79276]
<repo> that's a horrible code smell
<repo> as naqvis pointed out: please use `Channel`
<FromGitter> <Dan-Do> Okay, I am new :)
<repo> no problem :)
<repo> just a thing to keep in the back of your head: when you need sleep because otherwise something doesn't work (regardless of the programming language), you're probably doing it wrong
<FromGitter> <Dan-Do> how to avoid this? ⏎ ⏎ `````` [https://gitter.im/crystal-lang/crystal?at=5f6320d389b38d092132eda0]
<FromGitter> <Dan-Do> it blocks the program
<postmodern> why doesn't crystal have a fork() method?
<z64> postmodern: it has various complications; context: https://github.com/crystal-lang/crystal/issues/6421
<repo> interesting read
<z64> for a stable approach, you can consider managing processes and doing IPC by some other means. the spider-gazelle framework has this nice utility method that you might reference: https://github.com/spider-gazelle/action-controller/blob/master/src/action-controller/server.cr#L81-L150
<repo> z64: i think it'd be cool to have something like zeromq in native crystal
<repo> Dan-Do: maybe you should also explain in more detail what exactly you're after. I'm just assuming that you want to have a non blocking way of reading the lines from a process
<repo> (on stdout)
<FromGitter> <Dan-Do> I am creating a crystal program that interact with node cli. Normally we use STDIN & STDOUT of terminal
<FromGitter> <Dan-Do> I piped the nodejs process input & output using this ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f633683b468994d0d351402]
<FromGitter> <Dan-Do> this process wait for the input (terminated by \n), execute the script and return on it's output
<FromGitter> <Dan-Do> after flushing input, we don't know when process return string on output
<FromGitter> <Dan-Do> I am nearly there, just knew that we have to find a way to break the blocking loop :)
<repo> ok
<repo> well if you spawn and send the lines to a channel you should be safe
<FromGitter> <Dan-Do> we have to close the channel in the spawn block, don't we?
<FromGitter> <Dan-Do> I move it out then it blocks
sagax has joined #crystal-lang
<repo> yeah you'll need to close it after reading all lines so that the receive? returns nil
<repo> lines.close doesn't block
<repo> lines.receive? does
<repo> send also blocks
<repo> if you want to use it like a buffer, you will need to specify a capacity
<repo> so `Channel(String).new(1024)` for it to hold 1024 lines
<repo> then send won't block as long as the capacity isn't reached
<repo> but now that i think of what you actually try to do the following might be smarter: https://p.jokke.space/ZeRpN/
<repo> but don't you want to block at some point?
<repo> i mean somehow you'd have to wait for the process, right?
<FromGitter> <Dan-Do> The @node_process needs to run forever, so I cannot use `@node_process.wait`
<FromGitter> <Dan-Do> on the `spawn -> while` we don't know there are how many lines to read
alexherbo2 has joined #crystal-lang
<repo> ok in that case i'd say you just use a channel
zorp has joined #crystal-lang
<FromGitter> <Dan-Do> > *<repo>* ok in that case i'd say you just use a channel ⏎ ⏎ Thank you, I made it :)
<repo> np
<repo> next step: replace your node program with crystal ;)
<FromGitter> <Dan-Do> Really big step :)
<FromGitter> <Dan-Do> Actually I only need to render web front-end on the crystal server side then send to browser
<FromGitter> <Dan-Do> Just like https://github.com/straight-shoota/crinja
<FromGitter> <Dan-Do> Maybe I will ask him how to :)
<repo> why don't you then just use crinja?
<repo> in any case i'd go with compile-time template building
<repo> crinja does it on runtime
<FromGitter> <Blacksmoke16> @naqvis https://play.crystal-lang.org/#/r/9q4j
alexherbo22 has joined #crystal-lang
<FromGitter> <Dan-Do> > *<repo>* in any case i'd go with compile-time template building ⏎ ⏎ compile-time template is not flexible, I also want to build SPA on front-end but still server well when browser disable javascript. Moreover it's better for SEO
alexherbo2 has quit [Ping timeout: 240 seconds]
alexherbo22 is now known as alexherbo2
postmodern has quit [Quit: Leaving]
alexherbo27 has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 272 seconds]
alexherbo27 is now known as alexherbo2
alexherbo2 has quit [Ping timeout: 272 seconds]
alexherbo2 has joined #crystal-lang
<FromGitter> <extremety1989> hey guys
<FromGitter> <extremety1989> how can i create static method inside class ?
<FromGitter> <extremety1989> class Test
<FromGitter> <Blacksmoke16> `def self.foo`
<FromGitter> <extremety1989> and how to call it ?
<FromGitter> <extremety1989> Test.foo ?
<FromGitter> <Blacksmoke16> `Test.foo`?
<FromGitter> <Blacksmoke16> yes
<FromGitter> <extremety1989> 37 | result = self.Matrix.new(a.rows, b.cols) ⏎ ⏎ ``` ^-----``` ⏎ ⏎ Error: undefined method 'Matrix' for Matrix.class [https://gitter.im/crystal-lang/crystal?at=5f636feaf51808513b4c8bae]
<FromGitter> <extremety1989> i get error
<FromGitter> <extremety1989> matrix1 = Matrix.new 4,4 ⏎ matrix1.randomize ⏎ matrix2 = Matrix.new 4,4 ⏎ matrix2.randomize ⏎ res = Matrix.multiply(matrix1,matrix2) [https://gitter.im/crystal-lang/crystal?at=5f637011a9c2c8511e9f2c0e]
<FromGitter> <extremety1989> puts res
<FromGitter> <Blacksmoke16> try removing the `self.`
<FromGitter> <extremety1989> 64 | res = Matrix.multiply(matrix1,matrix2) ⏎ ⏎ ``` ^-------``` ⏎ ⏎ Error: undefined method 'multiply' for Matrix.class [https://gitter.im/crystal-lang/crystal?at=5f63705ddf4af236f9130d89]
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/cr can you make an example here?
<FromGitter> <Blacksmoke16> that'll be easier
<FromGitter> <extremety1989> https://play.crystal-lang.org/#/r/9q4m
<FromGitter> <Blacksmoke16> right, you have `def multiply`, not `def self.multiply`
<FromGitter> <Blacksmoke16> so atm its an instance method not a class method
<FromGitter> <extremety1989> how can i make it class method
<FromGitter> <Blacksmoke16> i just said, `def self.multiply`, prefix `self.` to the method name
<FromGitter> <extremety1989> check it out
<FromGitter> <extremety1989> it does not work
<FromGitter> <Blacksmoke16> you get a diff error
<FromGitter> <Blacksmoke16> `self.Matrix.new(a.rows, b.cols)` isn't right either, can just do `new a.rows, b.cols`
<FromGitter> <extremety1989> yeah
<FromGitter> <extremety1989> aa oke
<FromGitter> <extremety1989> gonna try
<FromGitter> <extremety1989> wait a moment
<FromGitter> <extremety1989> matrix1 = Matrix.new 4,4 ⏎ matrix1.randomize ⏎ matrix2 = Matrix.new 4,4 ⏎ matrix2.randomize ⏎ res = Matrix.multiply(matrix1,matrix2) ... [https://gitter.im/crystal-lang/crystal?at=5f6371843651184d30f8793c]
<FromGitter> <extremety1989> there is no self.Matrix
<FromGitter> <naqvis> > @naqvis https://play.crystal-lang.org/#/r/9q4j ⏎ ⏎ @Blacksmoke16 Thanks, much leaner approach, but that T doesn't constrain it to `Parent` level, one can still invoke that wither other classes. https://play.crystal-lang.org/#/r/9q4o ⏎ Mine was just an attempt to answer @mwlang question :P [https://gitter.im/crystal-lang/crystal?at=5f63719af51808513b4c9047]
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/9q4u fixed :slight_smile:
<FromGitter> <naqvis> 👍
<FromGitter> <Blacksmoke16> @extremety1989 not sure i follow?
<FromGitter> <extremety1989> yes
<FromGitter> <extremety1989> in javascript
<FromGitter> <extremety1989> you can create static class function and call it like So Tes.foo()
<FromGitter> <Blacksmoke16> yes, you can do that in crystal too
<FromGitter> <extremety1989> okey
<FromGitter> <extremety1989> works
<FromGitter> <extremety1989> i just create module
<FromGitter> <extremety1989> StaticMatrix
<FromGitter> <extremety1989> ahhaha
<FromGitter> <extremety1989> thanks
<FromGitter> <Blacksmoke16> ok
<FromGitter> <extremety1989> hmm
<FromGitter> <extremety1989> in crystal
<FromGitter> <extremety1989> i made rereturn
<FromGitter> <extremety1989> return data
<FromGitter> <extremety1989> i have class variable
<FromGitter> <extremety1989> i want to return it
<FromGitter> <extremety1989> 67 | puts res.data ⏎ ⏎ ``` ^---``` ⏎ ⏎ Error: undefined method 'data' for Nil (compile-time type is (Matrix | Nil)) [https://gitter.im/crystal-lang/crystal?at=5f6373e4ce5bbc7ffdcf1891]
<FromGitter> <Blacksmoke16> can you make another playground example?
<FromGitter> <extremety1989> https://play.crystal-lang.org/#/r/9q4v
<FromGitter> <Blacksmoke16> right, your static method can return `nil` or an instance of `Matrix`
<FromGitter> <Blacksmoke16> so you need to make sure its not nil
<FromGitter> <extremety1989> but i am returning instance
<FromGitter> <extremety1989> there is result = Matrix.new(a,b) inside StaticMatrix
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/9q4y
<FromGitter> <Blacksmoke16> ``` if a.cols != b.rows ⏎ return nil ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=5f6374e6df4af236f9131926]
<FromGitter> <Blacksmoke16> so `nil` is a possible return value to the compiler
<FromGitter> <Blacksmoke16> maybe an https://crystal-lang.org/api/master/ArgumentError.html would be better?
<FromGitter> <Blacksmoke16> :shrug:
<FromGitter> <Blacksmoke16> i also cleaned some things up a bit
<FromGitter> <extremety1989> aa okey
<FromGitter> <extremety1989> how to stop function ?
<FromGitter> <extremety1989> do not continue the function just exit the function ?
<FromGitter> <extremety1989> aa okey
<FromGitter> <extremety1989> i made it thank you for help :D
<FromGitter> <Blacksmoke16> 👍
<FromGitter> <extremety1989> hey are you there ?
<FromGitter> <Blacksmoke16> yes?
<FromGitter> <extremety1989> i went to the problem
<FromGitter> <extremety1989> https://play.crystal-lang.org/#/r/9q5n
<FromGitter> <Blacksmoke16> right, same issue we talked about before
<FromGitter> <Blacksmoke16> `multiply` can return nil, so its possible that variable is `nil`
<FromGitter> <extremety1989> it is not the same here
<FromGitter> <extremety1989> i have comments
<FromGitter> <extremety1989> hidden and bias are Matrix class type
<FromGitter> <extremety1989> they must have function add
<FromGitter> <extremety1989> but it is undefined
<FromGitter> <Blacksmoke16> > `multiply` can return nil, so its possible that variable is `nil`
<FromGitter> <Blacksmoke16> `add` doesn't exist on the `Nil` type
<FromGitter> <Blacksmoke16> so you need to make sure the value isn't nil before calling `.add`
<FromGitter> <Blacksmoke16> this is the compiler saving you from like `undefined method add on null`
<FromGitter> <Blacksmoke16> which you would get in js/ruby at runtime
<FromGitter> <Blacksmoke16> or instead of returning `nil`, raise an exception
<FromGitter> <extremety1989> aa okey
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/9q5o
<FromGitter> <extremety1989> thanks
<FromGitter> <extremety1989> when i use if else block
<FromGitter> <extremety1989> why it check the part of else when my condition is true ?
<FromGitter> <extremety1989> if 0 < 1
<FromGitter> <extremety1989> else
<FromGitter> <extremety1989> puts "no"
<FromGitter> <extremety1989> end
<FromGitter> <Blacksmoke16> hm?
<FromGitter> <Blacksmoke16> that doesnt print anything because there is nothing in the `if` block of it
<FromGitter> <extremety1989> def add(n) ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ end [https://gitter.im/crystal-lang/crystal?at=5f6391665fde567ffa531580]
<FromGitter> <extremety1989> oups
<FromGitter> <extremety1989> https://play.crystal-lang.org/#/r/9q5q
<FromGitter> <extremety1989> here
<FromGitter> <extremety1989> parameter n is of type matrix
<FromGitter> <extremety1989> but the else block executes the check
<FromGitter> <extremety1989> is it normal ?
<FromGitter> <extremety1989> 94 | @data[i][j] += n ⏎ ⏎ ``` ^``` ⏎ ⏎ Error: no overload matches 'Float64#+' with type Matrix [https://gitter.im/crystal-lang/crystal?at=5f63921f4f19dd0933564230]
<FromGitter> <extremety1989> it is on else block
<FromGitter> <Blacksmoke16> wew, got a simpler example?
<FromGitter> <Blacksmoke16> regardless i think you'd be better of using overloads
<FromGitter> <extremety1989> wait i will explain
<FromGitter> <extremety1989> the add function as parameter n
<FromGitter> <extremety1989> it can be of type Matrix or Int32
<FromGitter> <Blacksmoke16> tl;dr you want to run diff logic based on the type of the argument `n`
<FromGitter> <extremety1989> yes right !
<FromGitter> <extremety1989> now the n is Matrix
<FromGitter> <extremety1989> so my condition is true
<FromGitter> <Blacksmoke16> first off, do `n.is_a? Matrix`
<FromGitter> <Blacksmoke16> not `typeof(n) == Matrix`
<FromGitter> <extremety1989> works
<FromGitter> <extremety1989> thanks so much
<FromGitter> <Blacksmoke16> but theres a better way to do this sec
<FromGitter> <Blacksmoke16> try like
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f639318ce5bbc7ffdcf791b]
<FromGitter> <extremety1989> wow thanks
<FromGitter> <Blacksmoke16> also could you not just do like `@rows.times do |i|` instead of the while loops?
<FromGitter> <Blacksmoke16> and `@cols.times do |j|`
<FromGitter> <extremety1989> while loop is faster
<FromGitter> <Blacksmoke16> they're prob the same
<FromGitter> <Blacksmoke16> essentially does what you're doing, but in a cleaner way
<FromGitter> <extremety1989> seems complicated
<FromGitter> <extremety1989> :D
<FromGitter> <extremety1989> def map(execFunc) ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ end [https://gitter.im/crystal-lang/crystal?at=5f639433b468994d0d361c55]
<FromGitter> <extremety1989> how you do markdown ?
<FromGitter> <Blacksmoke16> in chat here?
<FromGitter> <Blacksmoke16> three backticks then hit enter, then paste your code
<FromGitter> <extremety1989> yes
<FromGitter> <extremety1989> ``````
<FromGitter> <extremety1989> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f6394934f19dd09335649ab]
<FromGitter> <extremety1989> lol
<FromGitter> <Blacksmoke16> has to be three on its own line, then your code, then three more on its own line
<FromGitter> <extremety1989> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f6394b8ce5bbc7ffdcf7df1]
<FromGitter> <Blacksmoke16> yes
<FromGitter> <Blacksmoke16> id still use `.times` instead tho
<FromGitter> <Blacksmoke16> more readable
<FromGitter> <extremety1989> map function takes function and executes
<FromGitter> <Blacksmoke16> so?
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f639548f969413294f04b84]
<FromGitter> <Blacksmoke16> is that not the same thing?
<FromGitter> <extremety1989> well
<FromGitter> <extremety1989> i want pass function
<FromGitter> <extremety1989> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f63959bf51808513b4cf9d6]
<FromGitter> <extremety1989> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f6395c2ce5bbc7ffdcf80f6]
<FromGitter> <extremety1989> 188 | hidden.map(relu) ⏎ ⏎ ``` ^---``` ⏎ ⏎ Error: wrong number of arguments for 'relu' (given 0, expected 1) ... [https://gitter.im/crystal-lang/crystal?at=5f6395ceea62d70e651ab1af]
<FromGitter> <Blacksmoke16> yea it doesnt work like that
<FromGitter> <extremety1989> i have found
<FromGitter> <extremety1989> my fault
<FromGitter> <extremety1989> omg
<FromGitter> <extremety1989> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f63971a89b38d09213430a9]
<FromGitter> <extremety1989> 213 | @@weights_ho.map(func) ⏎ ⏎ ``` ^``` ⏎ ⏎ Error: expecting token '}', not '@@weights_ho' [https://gitter.im/crystal-lang/crystal?at=5f639726a9c2c8511e9fa3f9]
<FromGitter> <extremety1989> why it is wrong ?
<FromGitter> <Blacksmoke16> you declared your function with a `{`?
<FromGitter> <extremety1989> aa
<FromGitter> <extremety1989> haha sorry
<FromGitter> <extremety1989> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f63984c3651184d30f8f13a]
<FromGitter> <extremety1989> not working as in documentation
<FromGitter> <Blacksmoke16> you need to use `->` on the actual method passed to `map`, not the argument
<FromGitter> <Blacksmoke16> or passed to `mutate` i guess
<FromGitter> <extremety1989> i do not get it D
<FromGitter> <Blacksmoke16> can you make a playground link?
<FromGitter> <extremety1989> wait i think now i uderstand
<FromGitter> <extremety1989> just let me do it bymyself
<FromGitter> <Blacksmoke16> okey dokey
<FromGitter> <extremety1989> @data: Array(Array(Float64))
<FromGitter> <extremety1989> here when i initialize 2d array
<FromGitter> <extremety1989> 80 | @data[i][j] = execFunc.call(@data[i][j]) ⏎ ⏎ ``` ^``` ⏎ ⏎ Error: no overload matches 'Array(Float64)#[]=' with types Int32, (Float64 | Int32) [https://gitter.im/crystal-lang/crystal?at=5f639d764f19dd09335666ad]
<FromGitter> <extremety1989> when i loop i and j is type of Int32 how can i fix this error ?
<FromGitter> <codic12> I have a method that accepts `::Bytes | Nil`, and i want to pass it a String, so I tried `"abc".bytes[..]` (the [..] for converting to a slice) but it still says that it's an Array of UInt8's
<FromGitter> <naqvis> @codic12 `"abc".to_slice`
<FromGitter> <codic12> ah, thanks!
<FromGitter> <naqvis> `#bytes` returns the string bytes as an `Array(UInt8)`
<FromGitter> <codic12> well, then I get this:
<FromGitter> <codic12> `Error: undefined method 'to_unsafe' for Nil (compile-time type is (Slice(UInt8) | Nil))`
<FromGitter> <naqvis> its because you are invoking this method on object which could be null
<FromGitter> <naqvis> so you need to handle cases where that object is null
<FromGitter> <codic12> ah, with begin rescue?
<FromGitter> <naqvis> no
<FromGitter> <naqvis> exceptions are runtime behavior
<FromGitter> <naqvis> but Nil handling is compile time behavior
<FromGitter> <naqvis> you need to either use `if var` or use `try`
<FromGitter> <codic12> try in the call?
<FromGitter> <naqvis> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f63a0a1f969413294f06e21]
<FromGitter> <naqvis> where `obj` is on what you are calling the `to_slice` method
<FromGitter> <naqvis> and if you are 100% confident that `obj` will never be nill and willing to accept runtime exceptions then go with `obj.not_nil!.to_slice`
<FromGitter> <naqvis> i won't suggest this, because in this way you are telling compiler to leave you alone and you know what you are doing :P
<FromGitter> <codic12> Passing in `"abc".not_nil!.to_slice` doesn't work; so my full line is now `term.feed("abc".not_nil!.to_slice)`, but I get the same erro
<FromGitter> <codic12> It's hardcoded in the code so I'm sure it will not be nil
<FromGitter> <naqvis> if hardcoded, then you needn't to call `not_nil!` :P
<FromGitter> <naqvis> you better make a playground link
<FromGitter> <naqvis> https://carc.in/#/r/9q63
<FromGitter> <codic12> It's actually using the gobject bindings for crytal; the full error is https://hastebin.com/hogoyevabe.coffeescript
<FromGitter> <naqvis> `def feed(data : ::Bytes?)`
<FromGitter> <naqvis> in method signature you are stating that data could be either `Bytes` or `Nil`
<FromGitter> <naqvis> so compiler is definitely right :)
<FromGitter> <codic12> Yeah, those bindings are autogenerated ⏎ So I can work arond this with `term.try term.feed("abc".to_slice)`?
<FromGitter> <naqvis> `term` could be nil?
<FromGitter> <codic12> so I somehow try `term`?
<FromGitter> <naqvis> you better make a playground link and try to create the scenario where you are experiencing problem
<FromGitter> <codic12> does playground allow using external libraries though?
<krobin> ah, damn. sorry, copy & paste fail
<krobin> cd
<FromGitter> <Blacksmoke16> *hacked*
<FromGitter> <naqvis> :P
<FromGitter> <naqvis> i saw the password its `******`
<FromGitter> <codic12> Here's a recreation: https://play.crystal-lang.org/#/r/9q6k ⏎ But for some reason, it does work here
<FromGitter> <extremety1989> ahahha
<FromGitter> <codic12> although, to_unsafe is called on the object, one sec
<FromGitter> <extremety1989> @Blacksmoke16 you there ?
<FromGitter> <Blacksmoke16> yes
<FromGitter> <codic12> there we go
<FromGitter> <extremety1989> @Blacksmoke16 hey
<FromGitter> <extremety1989> have not worked
<FromGitter> <RespiteSage> https://play.crystal-lang.org/#/r/9q6o ?
<FromGitter> <Blacksmoke16> @extremety1989 oh?
<FromGitter> <RespiteSage> The error you were getting is just that `Nil` has no `#to_unsafe` method.
<FromGitter> <RespiteSage> And it was possible for that ternary operator to return `nil`.
<FromGitter> <naqvis> @codic12 `puts (text ? text : nil).to_unsafe`
<FromGitter> <naqvis> you are returning `nil`
<FromGitter> <RespiteSage> ^
<FromGitter> <RespiteSage> Or https://play.crystal-lang.org/#/r/9q6s
<FromGitter> <codic12> Yeah, but this code is autogenerated, I have no control over it
<FromGitter> <codic12> I'll file a bug upstream
<FromGitter> <codic12> Thanks!
<FromGitter> <RespiteSage> More explicitly (which I sometimes do if code might be read by someone who's not used to Crystal or Ruby): https://play.crystal-lang.org/#/r/9q6u
<FromGitter> <RespiteSage> No problem. Hope that issue gets fixed for you.
<FromGitter> <grkek> Crystal 1.0.0 when?
<FromGitter> <grkek> also I added a preview of the Control gallery to my UI framework https://github.com/grkek/iu
<FromGitter> <grkek> :)
<FromGitter> <extremety1989> @Blacksmoke16 i made it :D
<FromGitter> <Blacksmoke16> 👍 nice!
<FromGitter> <extremety1989> how can i make
<FromGitter> <extremety1989> from
<FromGitter> <extremety1989> [[1,2,3]] to [1,2,3]
<FromGitter> <extremety1989> ?
<FromGitter> <Blacksmoke16> `.first`?
<FromGitter> <grkek> [[1,2,3]].[0]
<FromGitter> <grkek> export the flatten code to a function
<FromGitter> <naqvis> `.flatten`
<FromGitter> <grkek> flatten([[1, 2, 3]])
<FromGitter> <Blacksmoke16> good call
<FromGitter> <naqvis> `[[1,2,3],[4,5,6]] .flatten # => [1, 2, 3, 4, 5, 6]`
<FromGitter> <extremety1989> thanks
<FromGitter> <extremety1989> :D
<FromGitter> <extremety1989> you are the best
<FromGitter> <extremety1989> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f63b05fb468994d0d367258]
<FromGitter> <extremety1989> 229 | return sqrt( -2.0 * log( u ) ) * cos( 2.0 * PI * v ) ⏎ ⏎ ``` ^--``` ⏎ ⏎ Error: undefined method 'log' for top-level [https://gitter.im/crystal-lang/crystal?at=5f63b071a857200e6d68c287]
<FromGitter> <Blacksmoke16> its prob `Math.log`?
<FromGitter> <extremety1989> yea
<FromGitter> <extremety1989> aa
<FromGitter> <extremety1989> in crystal also ?
<FromGitter> <extremety1989> i thought in javascript only
<FromGitter> <extremety1989> :D
<FromGitter> <extremety1989> i know
<FromGitter> <extremety1989> but it was something strange
<FromGitter> <extremety1989> weirdness
<FromGitter> <extremety1989> 229 | return Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * PI * v ) ⏎ ⏎ ``` ^``` ⏎ ⏎ Error: undefined constant PI [https://gitter.im/crystal-lang/crystal?at=5f63b0f2603d0b37f43b237e]
<FromGitter> <Blacksmoke16> right... `Math::PI`
<FromGitter> <extremety1989> wow works perfectly
<FromGitter> <extremety1989> thanks
alexherbo2 has quit [Quit: Ping timeout (120 seconds)]
alexherbo2 has joined #crystal-lang
postmodern has joined #crystal-lang
<deimos_> hi, question i can't find an answer to in the docs or source (or maybe i must misunderstood the source). about crystal-db, trying to query 100k items in a hash against psql, it seems crystal-db is doing the queries serially, rather than using the pool size (50). is this correct?
<deimos_> and should I be able to use Fibers to make this sort of threaded, or at least use up the pool size to run 50 queries at a time?
<FromGitter> <Blacksmoke16> got some code to share?
<FromGitter> <Blacksmoke16> afaik (correct me if im wrong) the pool is to allow multiple connections to the db, think two separate http requests
<FromGitter> <Blacksmoke16> the pool isnt there to allow a single connection to do multiple queries in parallel (but there are some methods defined that do this)
<deimos_> that's what i thought aobut the pool too, it's there to "parallelize" the queries up to the pool limit
<FromGitter> <Blacksmoke16> it also depends on what methods you're using, some are scoped to a specific connection, others allow getting a connection from the pool
<FromGitter> <Blacksmoke16> if you can share some code i can take al ook
<deimos_> i'm using db_scalar calls, because the query results should be 1 value
<deimos_> i'm working up sanitized code to show
<FromGitter> <Blacksmoke16> 👍 perfect
<deimos_> this is close enough, https://dpaste.org/w5nY
<FromGitter> <Blacksmoke16> alright good so you are using `db`
<deimos_> there's plenty of other thigns i can clean up
<FromGitter> <Blacksmoke16> are you passing anything to `.open`? there are some query params
<FromGitter> <Blacksmoke16> to control the pool
<deimos_> no, i took the default, which i think is 5
<deimos_> err, it's 1 pooled connection by default, unlimited pool_size
<deimos_> psql is limited to 50 per connection
<deimos_> so in theory, pool_size should grow to 50
<FromGitter> <Blacksmoke16> could try upping the initial size and see if that makes a diff
<deimos_> err, max_pool_size
<FromGitter> <Blacksmoke16> otherwise, fibers would prob be the way to go
<FromGitter> <Blacksmoke16> the db pool afaik is more focused around the connections to the db, i.e. like if you had two requests come in at the same time that needed to run this code
<deimos_> if this was ruby, i'd use the parallel gem to run Parallel.each(customer_details) do |cust|
<deimos_> which does fill the pool and keeps it full until the each was exhausted
<FromGitter> <Blacksmoke16> again the db pool i think is more so for the connections of the db itself, not as an alternative for you to have concurrent code persay
<FromGitter> <Blacksmoke16> id do something like
<deimos_> right, that's why i thought of fibers
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5f63e35ca857200e6d694df3]
<FromGitter> <Blacksmoke16> ah might need to add `cust` or something to the channel
<FromGitter> <Blacksmoke16> didnt see that in the hash setting
<deimos_> wow, thanks
<deimos_> still, this gives me ideas for using fibers
<FromGitter> <Blacksmoke16> untested and prob should also handle errors and such but its a start
<deimos_> i like your usage of raise "msg" if
<FromGitter> <Blacksmoke16> guard clauses are nice yes :)
<deimos_> but other devs get cranky with me when it's not "if condition then action else other action"
<FromGitter> <Blacksmoke16> both have their places
<deimos_> and i can only get into so many religious fights
<FromGitter> <Blacksmoke16> this prevents the need for another level of indent
<FromGitter> <Blacksmoke16> for pretty much no reason
<deimos_> yeah, i use a lot of condition ? action : other action
<deimos_> which also drives some people crazy
<FromGitter> <Blacksmoke16> depends on context
<FromGitter> <Blacksmoke16> can be a good thing, but gets messy when its long/multiple statements/nested