jhass changed the topic of #crystal-lang to: The Crystal programming language | http://crystal-lang.org | Crystal 0.28.0 | 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
<FromGitter> <girng> good night!
<FromGitter> <girng> now i have to leave in 30 minutes :( maybe i'll brainstorm ideas while at work, then, when i get home, implement them in code. so i don't waste time at home thinking about ideas. i can think of ideas anywhere!
<FromGitter> <tenebrousedge> Definitely. I just read a book on cancer where it seemed like every breakthrough was the product of someone thinking on their way to work. Other people have their world-changing insights in the shower
<FromGitter> <tenebrousedge> Archimedes did something similar
<FromGitter> <Blacksmoke16> makes sense considering those times where there is nothing else to do but think :p
<FromGitter> <Blacksmoke16> since the actual action of driving or taking a shower takes trivial amounts of brain function
<FromGitter> <Blacksmoke16> helps pass the time
<FromGitter> <girng> @tenebrousedge funny you mentioned that. i've actually thought of some game-dev related ideas in the shower or doing the dishes at home. and implemented them in crystal the next day
<FromGitter> <girng> just out of nowhere too, very weird
<FromGitter> <girng> the ideas would randomly pop up in my head
<FromGitter> <girng> light bulb moment
<FromGitter> <Daniel-Worrall> Is there a way I can coerce the arrays in this hash? https://carc.in/#/r/7039
<FromGitter> <girng> @Blacksmoke16 true
<FromGitter> <Daniel-Worrall> The hash is gonna have a lot of values so it'll be silly to have to _u64 every int
<FromGitter> <Blacksmoke16> add `_u64` to end of the number
<FromGitter> <Daniel-Worrall> After *every* number though...
<FromGitter> <Blacksmoke16> are you manually typeing in the numbers or?
<FromGitter> <Daniel-Worrall> atm, yes
<FromGitter> <Blacksmoke16> `[123].map(&.to_u64)`
<FromGitter> <Daniel-Worrall> that's still a lot
<FromGitter> <Blacksmoke16> well what are you expecting? you're giving it an Int32 but wanting it to be typed to UInt64
<FromGitter> <Blacksmoke16> you'll have to do something
<FromGitter> <Daniel-Worrall> I'll just _u64 it all
<FromGitter> <Blacksmoke16> πŸ‘
<FromGitter> <girng> πŸ‘
<FromGitter> <tenebrousedge> what they said
<FromGitter> <Blacksmoke16> other option is like `UInt64.new(123)`
<FromGitter> <Daniel-Worrall> because https://carc.in/#/r/703f works
<FromGitter> <Daniel-Worrall> It's the fact they're in arrays
<FromGitter> <Blacksmoke16> in that case it prob gets autocated
<FromGitter> <girng> blacksmoke always in here helping others. it's really amazing
<FromGitter> <Blacksmoke16> but im thinking it cant auto cast it when its in an array since it would be of a diff type
<FromGitter> <Daniel-Worrall> I think Crystal could handle this case, but it doesn't atm
<FromGitter> <Blacksmoke16> actually hmm
<FromGitter> <asterite> of UInt64 after the array literal
<FromGitter> <Blacksmoke16> lol my bad
<FromGitter> <Blacksmoke16> you can just do that ^
<FromGitter> <Blacksmoke16> just about to say that ;P
<FromGitter> <Blacksmoke16> `[123] of UInt64`
<FromGitter> <Blacksmoke16> if you dont tell it the array is of type Array(Int32) which isnt valid based on your type restriction of Array(UInt64) but that tells it the array it should treat values in it as UInt64
<FromGitter> <Daniel-Worrall> yeah, but that's a lot of code for each line of the hash
<FromGitter> <Daniel-Worrall> I was hoping to handle it outside the contents
<FromGitter> <Blacksmoke16> are you pushing the values into it? or creating it with them already in there?
<FromGitter> <Daniel-Worrall> creating
<FromGitter> <Daniel-Worrall> I guess I could iterate and push them on a different hash
<FromGitter> <Blacksmoke16> sec
<FromGitter> <Daniel-Worrall> but I've changed them all now, it's fine
<FromGitter> <Blacksmoke16> fair enough
<FromGitter> <watzon> Where is the documentation on how `with self yield` works?
<FromGitter> <watzon> @vladfaust it was you that suggested that rather than writing another API wrapper I write something that simplifies writing API wrappers right?
laaron has quit [Remote host closed the connection]
<FromGitter> <watzon> Or something along those lines
laaron has joined #crystal-lang
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
beepdog has quit [Remote host closed the connection]
olbat[m] has quit [Read error: Connection reset by peer]
DTZUZU2 has quit [Ping timeout: 250 seconds]
olbat[m] has joined #crystal-lang
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
DTZUZU2 has joined #crystal-lang
<FromGitter> <vladfaust> Yes, it was my suggestion
<FromGitter> <alex-lairan> @bew thanks for the review !
ShalokShalom has joined #crystal-lang
<FromGitter> <watzon> @vladfaust I'm working on something πŸ˜‰
<FromGitter> <vladfaust> That’s awesome. I could help on the arch. Invite me or publish the scratch as-is
<FromGitter> <watzon> @vladfaust https://github.com/watzon/wapi that's what I have right now. Still working on the architecture. I'm trying to do Documentation Driven Development, so I'm writing docs first for most things and then writing the code to match.
<FromGitter> <watzon> I'm basing the idea on the ApiStruct rubygem somewhat
<FromGitter> <watzon> It's not possible to pass something by reference in Crystal is it? Just realized that it doesn't seem to be supported lol.
<FromGitter> <alex-lairan> @watzon classes are passed by reference, struct by copy
<FromGitter> <watzon> What I'm wondering is: what would be the best syntax for a `before_hook` which runs before a HTTP request and might modify the verb, uri, or options (in this case Halite::Options`. It would be nice if they didn't have to return anything from the hook and they could just modify the variables directly like you could do if you were passing by reference in a language like C++ or Rust.
<FromGitter> <girng> Hello all!
<FromGitter> <girng> How is everyone doing
<FromGitter> <vladfaust> The project 1) needs an example of somewhat implemented wrapper, e.g. GitHub 2) must allow to use multiple API wrappers in a single app ⏎ ⏎ I don't get that assertion thing. ⏎ ⏎ Also you should not rely neither on Ruby implementation not on NodeJS examples. Crystal has its own philosophy and code practice. I don't understand why are you reinventing the wheel with custom logger
<FromGitter> <vladfaust> > How is everyone doing ⏎ ⏎ Well, I'm writing my own Linux OS for Worcr πŸ˜…
<FromGitter> <watzon> I like having my own logger haha. Gives me more flexibility. Eventually I'll probably refactor it into it's own shard.
<FromGitter> <watzon> As far as examples go I have one, I just haven't committed it yet
<FromGitter> <vladfaust> If I implemented this, I'd make wrapper an abstract class. That *config* thing is Rails-ish rot
<FromGitter> <vladfaust> OOP, more OOP, my friend
<FromGitter> <watzon> What would be a more crystally way to do configuration?
<FromGitter> <watzon> I'm all ears because I've been having trouble in that department
<FromGitter> <vladfaust> Abstract class, modules, settings on initialize
laaron has quit [Remote host closed the connection]
<FromGitter> <watzon> Ahh I see. So make Wrapi abstract so that it can be extended that way.
<FromGitter> <watzon> That makes sense
<FromGitter> <vladfaust> Also properties to change the class on the fly
<FromGitter> <vladfaust> ```def self.new(&block) ⏎ yield new ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=5cee3af9481ef4167b957a06]
laaron has joined #crystal-lang
<FromGitter> <vladfaust> And stick to Crystal primitives. Default logger, for example
<FromGitter> <vladfaust> I want to choose which logging to use. Maybe I want Onyx logger, or Blacksmoke's
<FromGitter> <watzon> True
<FromGitter> <vladfaust> I also encourage you to use default HTTP client, i.e. have minimum deps
<FromGitter> <vladfaust> I'll skip invitation for now, because you already have the code published
<FromGitter> <watzon> Gotcha. I'll take all of that into consideration :)
<FromGitter> <vladfaust> Good. And the last for now -- always stick to certain version constraints in `shards.yml`. I see some `master` thing there
<FromGitter> <alex-lairan> > I like having my own logger haha. Gives me more flexibility. ⏎ ⏎ No, DI will give you this flexibility. ⏎ ⏎ Re-create it will lead to less flexibility, and maintenance :) [https://gitter.im/crystal-lang/crystal?at=5cee41957c1dab468c663648]
ShalokShalom has quit [Remote host closed the connection]
rohitpaulk has joined #crystal-lang
<FromGitter> <davidcolombogit> I have a question guys ⏎ ⏎ I query a SQLite3 database and have in ``` rs.each do ``` ⏎ The following code ⏎ ... [https://gitter.im/crystal-lang/crystal?at=5cee44119b50f63935489790]
ashirase has quit [Ping timeout: 246 seconds]
ashirase has joined #crystal-lang
DTZUZU has quit [Ping timeout: 258 seconds]
<FromGitter> <kaukas_gitlab> `rs.read` is a destructive operation, it moves a pointer.
rohitpaulk has quit [Ping timeout: 248 seconds]
rohitpaulk has joined #crystal-lang
alex`` has joined #crystal-lang
<alex``> Hi
<alex``> How to write to a tempfile once created?
<alex``> tempfile = File.tempfile('foo')
<alex``> "foo"*
<alex``> is it the same than Ruby, tempfile.write("contents")?
<alex``> or with a block?
alex`` has quit [Ping timeout: 245 seconds]
alex`` has joined #crystal-lang
alex`` has quit [Ping timeout: 246 seconds]
alex`` has joined #crystal-lang
<FromGitter> <davidcolombogit> @kaukas_gitlab
<FromGitter> <davidcolombogit> @kaukas_gitlab thanks
<FromGitter> <drum445> How can I convert a hash of (String, JSON::Any) into the actual data types please?
<FromGitter> <Blacksmoke16> @alex its an IO so you can do like `file.puts` or whatever
<FromGitter> <Blacksmoke16> or you can also use the block form like https://crystal-lang.org/api/0.28.0/File.html#tempfile%28suffix%3AString%3F%3Dnil%2C%2A%2Cdir%3AString%3DDir.tempdir%2Cencoding%3Dnil%2Cinvalid%3Dnil%2C%26block%29-class-method
<FromGitter> <Blacksmoke16> @drum445 good luck
<FromGitter> <Blacksmoke16> can you not use the `JSON::Serializable` stuff instead?
return0e has quit [Remote host closed the connection]
rohitpaulk has quit [Ping timeout: 244 seconds]
<FromGitter> <Blacksmoke16> @watzon uh oh, is my logger going to get some competition? :P
rohitpaulk has joined #crystal-lang
return0e has joined #crystal-lang
rohitpaulk has quit [Ping timeout: 258 seconds]
rohitpaulk has joined #crystal-lang
DTZUZU has joined #crystal-lang
<FromGitter> <pynixwang> how to convert a JSON::Any to data type?
<FromGitter> <pynixwang> can't cast JSON::Any to Int64
<FromGitter> <pynixwang> JSON.parse("1").as(Int64)
<FromGitter> <Blacksmoke16> `as_i64`
<FromGitter> <pynixwang> but non basic types?
<FromGitter> <Blacksmoke16> you need to tell it what the type should be, using .as doesnt do that
<FromGitter> <pynixwang> I use to_json then from_json
<FromGitter> <Blacksmoke16> if you're using from_json you can use the serializable stuff
<FromGitter> <pynixwang> can cause performence issus
<FromGitter> <Blacksmoke16> and not have to deal with json any
<FromGitter> <pynixwang> I have a struct with some properties and a JSON:Any property
rohitpaulk has quit [Remote host closed the connection]
<FromGitter> <pynixwang> now I want to convert this property to a type
<FromGitter> <pynixwang> seems jsonrpc
moei has joined #crystal-lang
<FromGitter> <pynixwang> I must see if error
<FromGitter> <pynixwang> if not, cast result to type.
<FromGitter> <Blacksmoke16> got some example code?
alex`` has quit [Ping timeout: 245 seconds]
alex`` has joined #crystal-lang
<FromGitter> <pynixwang> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5cee94d06fc5846bab2a5da6]
<FromGitter> <pynixwang> this is a jsonrpc response
<FromGitter> <Blacksmoke16> whats the actual data look like for those properties?
<FromGitter> <Blacksmoke16> normally you would use another object
<FromGitter> <pynixwang> result can be some type.
<FromGitter> <Blacksmoke16> that could like have like `code, message, description`
<FromGitter> <pynixwang> I want to decode next step
<FromGitter> <Blacksmoke16> what does the json look like for each?
<FromGitter> <Blacksmoke16> im assuming its in some standard structure?
<FromGitter> <pynixwang> I will check error nil and decode it
<jhass> might just keep the original json around and parse twice
<FromGitter> <pynixwang> ```{ ⏎ "result": 1, ⏎ "error": null ⏎ }``` [https://gitter.im/crystal-lang/crystal?at=5cee95529b50f639354ab5b9]
<FromGitter> <pynixwang> this is only one sense
<FromGitter> <Blacksmoke16> i mean when its not null
<jhass> Result.from_json(json) unless Error.from_json(json).is_error?
<FromGitter> <pynixwang> yes
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5cee958c7c1dab468c687d77]
<FromGitter> <Blacksmoke16> is it like something like that or?
<FromGitter> <pynixwang> error not nill result will be nil
<FromGitter> <pynixwang> yes
<FromGitter> <Blacksmoke16> so why not just do like
<FromGitter> <pynixwang> jsonrpc
<FromGitter> <pynixwang> ether result or error will be nil
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5cee95cc9b50f639354aba2a]
<FromGitter> <pynixwang> then I will cast result to a type
<FromGitter> <pynixwang> if error is nil
<FromGitter> <pynixwang> but it is JSON:Any now
<FromGitter> <pynixwang> I can not Type.from_json(result) .
<FromGitter> <pynixwang> and `as` also not work
<FromGitter> <Blacksmoke16> i would just avoid json any if you can
<FromGitter> <pynixwang> now I Type.form_json(result.to_json)
<FromGitter> <Blacksmoke16> πŸ˜•
<FromGitter> <pynixwang> this deserialize tiwce
<FromGitter> <pynixwang> and I want a way to cast result to Type
<FromGitter> <Blacksmoke16> ```code paste, see link``` ⏎ ⏎ I'm just confused why you cant do like `Response.from_json json_str` [https://gitter.im/crystal-lang/crystal?at=5cee96e7481ef4167b97f08b]
<FromGitter> <pynixwang> this is a way
<FromGitter> <pynixwang> but I must define some TypeResponse
<FromGitter> <pynixwang> because Type is not only Int32
<FromGitter> <Blacksmoke16> is it of any type?
<FromGitter> <pynixwang> maybe
<FromGitter> <Blacksmoke16> how would you know what the type of it is?
<FromGitter> <pynixwang> from method return type
<FromGitter> <Blacksmoke16> so like `get_x` it would be a Int32, but `get_y` could be a String?
<FromGitter> <pynixwang> can I define TypeResponse in a macro
<FromGitter> <Blacksmoke16> couldn't you just make `Response` a generic and do like
<FromGitter> <pynixwang> this sounds good
<FromGitter> <Blacksmoke16> ```code paste, see link``` ⏎ ⏎ Then use it like `Response(Int32).from_json json_str` [https://gitter.im/crystal-lang/crystal?at=5cee97f59b50f639354ac839]
<FromGitter> <pynixwang> I will try
<FromGitter> <Blacksmoke16> then each method could define its own type that the result should be
<FromGitter> <pynixwang> but T must optional now
<FromGitter> <Blacksmoke16> will `result` be null if there was an error?
<FromGitter> <pynixwang> I can not check nil error first
<FromGitter> <pynixwang> try.
<FromGitter> <pynixwang> but not so directly.
<FromGitter> <pynixwang> feel
<FromGitter> <pynixwang> thanks .
<jhass> you can do T?
<jhass> union of T and Nil
<jhass> making the field optional
<FromGitter> <pynixwang> ok
<FromGitter> <pynixwang> thx
<FromGitter> <cserb> Hi, is there a way to get the hexadecimal value of a BigInt? Maybe even as a string. Like `BigInt.new(32).to_hex # => 0x20`
<FromGitter> <pynixwang> override from_json of BigInt
<FromGitter> <pynixwang> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5ceea2d9702b7e5e76f65b2e]
<FromGitter> <pynixwang> case kind
<FromGitter> <Blacksmoke16> whats the point of doing tha?
<FromGitter> <pynixwang> maybe web3 rpc return hex
<FromGitter> <Blacksmoke16> `BigInt.new(32).to_s(16)` @cserb
<FromGitter> <Blacksmoke16> should be what you want
<FromGitter> <pynixwang> oh.
<FromGitter> <pynixwang> i get error
<FromGitter> <pynixwang> not deserialzie
<FromGitter> <Blacksmoke16> hm?
<FromGitter> <cserb> @Blacksmoke16 I totally forgot about `to_s(16)`. Thanks!
<FromGitter> <Blacksmoke16> np
<FromGitter> <asterite> @Blacksmoke16 I'm not sure what was agreed in https://github.com/crystal-lang/crystal/pull/6989 and I won't have time to do anything related to Crystal for about two weeks, so...
<FromGitter> <Blacksmoke16> as i said in my comment, it seems https://github.com/crystal-lang/crystal/pull/6989#issuecomment-433044313 is a good start. would be nice to have at least something then can fine tune it as needed as it gets some usage
moei has quit [Ping timeout: 248 seconds]
<FromGitter> <pynixwang> `to_s` call `to_s(io)`?
<FromGitter> <Blacksmoke16> the first just returns the string representation of what you use it on, the latter writes the string to the given io
<FromGitter> <pynixwang> but type always override to_s(io)
alex`` has quit [Read error: Connection reset by peer]
return0e has quit [Read error: Connection reset by peer]
return0e has joined #crystal-lang
alex`` has joined #crystal-lang
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
<FromGitter> <girng> apparently you can't have `class Error` https://play.crystal-lang.org/#/r/708f
<FromGitter> <girng> Change Error to Errorr and it works fine https://play.crystal-lang.org/#/r/708g
<FromGitter> <girng> @pynixwang
<FromGitter> <Blacksmoke16> its already a class
<FromGitter> <Blacksmoke16> id rename it to something more clear, like `ResponseError` or something
<FromGitter> <girng> yah
<FromGitter> <girng> @pynixwang if you still getting issues just do `JSON.parse(jsonstring)` it makes your life easier with complex json data, but i mean if you already know the data beforehand, stick to what blacksmoke has given u
rohitpaulk has joined #crystal-lang
rohitpaulk has quit [Ping timeout: 244 seconds]
griddle has left #crystal-lang ["The Lounge - https://thelounge.chat"]
laaron- has joined #crystal-lang
laaron has quit [Quit: ZNC 1.7.1 - https://znc.in]
andrzejku has joined #crystal-lang
<andrzejku> hello
<FromGitter> <Blacksmoke16> o/
alex`` has quit [Read error: Connection reset by peer]
<FromGitter> <r00ster91> how does it work that I can create a struct/class with arguments of a JSON mapping?
alex`` has joined #crystal-lang
<FromGitter> <r00ster91> ```code paste, see link``` ⏎ ⏎ I want to be able to do this [https://gitter.im/crystal-lang/crystal?at=5ceee5b09b50f639354cfd29]
<FromGitter> <Blacksmoke16> πŸ˜•
<FromGitter> <Blacksmoke16> oh i see
<z64> `def initialize(@argument : String)`
<FromGitter> <Blacksmoke16> ^
<FromGitter> <r00ster91> oh I need to write down all the arguments and the types again
<z64> if you want a slightly more concise way with some tradeoffs, you can use `record A, .. { include JSON::Serializable }` https://carc.in/#/r/7092
<FromGitter> <Blacksmoke16> records ftw
<FromGitter> <r00ster91> hmm the `def initialize` thing doesn't work, I'm still getting a (given 2, expected 1) error. I think the macro is overwriting the `def initialize`
<z64> works fine here https://carc.in/#/r/7090 ; its what we do in several places in dcr
<FromGitter> <r00ster91> aah I misread `PrivateChannel` as `PartialChannel` the whole time. that was the problem
<FromGitter> <Blacksmoke16> :P that would do it
<FromGitter> <girng> hi rooster
andrzejku has quit [Remote host closed the connection]
<FromGitter> <r00ster91> hi
DTZUZU has quit [Ping timeout: 245 seconds]
DTZUZU has joined #crystal-lang
teardown has quit [Read error: Connection reset by peer]
teardown has joined #crystal-lang
sz0 has joined #crystal-lang
wymillerlinux has quit [Read error: Connection reset by peer]
alex`` has quit [Ping timeout: 250 seconds]
alex`` has joined #crystal-lang
<FromGitter> <watzon> What's the best DI pattern for Crystal? I'm sure @Blacksmoke16 will have something to say on that topic.
wymillerlinux has joined #crystal-lang
<FromGitter> <Daniel-Worrall> https://carc.in/#/r/709o Is this a good way of caching a string that might be empty? (not nil) from an API
<FromGitter> <drosehn> It's fine, afaik. Not sure why you'd capture it in method `foo` instead of in `initialize`. Mainly it seems odd to me that a method to return the value of `@foo` would ever modify the value as kept inside the object.
<FromGitter> <Daniel-Worrall> so it can lazily fetch it from an API
<FromGitter> <drosehn> If it were me, I might have the `foo` method *return* "foo" to the caller, but I wouldn't have it modify the value of `@foo`. Just my opinion.
<FromGitter> <Daniel-Worrall> It's going to be a class built from JSON where a rare case makes the string empty and so if it is empty, you need to fetch it from a different API call, but I want to do it lazily instead of on creation
<FromGitter> <drosehn> In any case, `foo.empty?` seems fine. Sometimes I'd do `foo.size == 0`, although I can't say that would be better.
<FromGitter> <Daniel-Worrall> Normally I would just use getter
<FromGitter> <drosehn> yeah, but the standard getter wouldn't work for this case. If you have a lot of values like this one, you could probably write a crystal macro to do most the work for you, and then use `alt_getter` instead of the standard `getter`.
<FromGitter> <Daniel-Worrall> Na, just this one
<FromGitter> <Blacksmoke16> @watzon https://github.com/Blacksmoke16/athena/blob/di/docs/dependency_injection.md is on a working branch tho.
<FromGitter> <Blacksmoke16> can be used outside athena as well
<FromGitter> <Blacksmoke16> but if you have any exp with id be happy to talk it over with you, are some things id like to do but am kinda out of ideas
teardown has quit [Quit: leaving]