RX14 changed the topic of #crystal-lang to: The Crystal programming language | http://crystal-lang.org | Crystal 0.26.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
DTZUZO has joined #crystal-lang
non-aristotelian has quit [Quit: non-aristotelian]
pabs has quit [Ping timeout: 246 seconds]
Heaven31415 has quit [Quit: Leaving]
rohitpaulk has joined #crystal-lang
moei has quit [Ping timeout: 244 seconds]
llua has quit [Remote host closed the connection]
llua has joined #crystal-lang
oz has quit [Ping timeout: 252 seconds]
oz has joined #crystal-lang
jsn- has quit [Ping timeout: 244 seconds]
Yxhuvud has quit [Ping timeout: 244 seconds]
<FromGitter> <j8r> No need to blockchain @bajro17 , like mastodon but for git πŸ˜ƒ
moei has joined #crystal-lang
<FromGitter> <kingsleyh> Git is already kind of decentralised
<FromGitter> <j8r> yes, indeed, but what Gitlab, Github and Mastodon brings is the social part
rohitpaulk has quit [Ping timeout: 250 seconds]
Yxhuvud has joined #crystal-lang
rohitpaulk has joined #crystal-lang
rohitpaulk has quit [Ping timeout: 246 seconds]
rohitpaulk has joined #crystal-lang
ashirase has quit [Ping timeout: 250 seconds]
ashirase has joined #crystal-lang
<FromGitter> <drum445> Hello, is there anyway to ready a sql result set into a hash dynamically, similar to how Python has dict cursors?
sagax has quit [Remote host closed the connection]
rohitpaulk has quit [Ping timeout: 250 seconds]
<FromGitter> <aboeglin> @drum445 if you figure it out I'd be interested too, it'd also make the code better to read a whole row
<FromGitter> <drum445> shall do sir, I wrote this lib https://github.com/drum445/objectify to easily transform it to a class, but a hash would be nice
<FromGitter> <drum445> I might look into it
<FromGitter> <aboeglin> Something I haven't try but worth trying would be rs.read(NamedTuple(colName : colType, ...))
<FromGitter> <aboeglin> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5bc07a0d1c100a4f29728d55]
<FromGitter> <aboeglin> I found that
<FromGitter> <aboeglin> A NamedTuple is pretty much a Hash, except the structure is fixed, but it's not like the structure of your table will change during execution so it should be fine and more suitable than a Hash for that task.
<FromGitter> <j8r> or else a struct
<FromGitter> <aboeglin> @drum445 so basically the little example I put above should be valid, I'll try that when I get a chance, you have an overload for un-named tuples as well, so you'd just give it the types without names, so you'd have the respect the order of the columns like you probably do right now.
<FromGitter> <aboeglin> I'm not sure you have splats for struct or hash
<FromGitter> <aboeglin> I think it's pretty much a tuple operator
<FromGitter> <j8r> for `**` yes
<FromGitter> <j8r> `**` namedtuple, and `*` tuple
<FromGitter> <fusillicode_twitter> @j8r I'm wondering why structs are usually favored & suggested in place of tuples πŸ€”
<FromGitter> <fusillicode_twitter> personally I find named tuples more flexible to use when you're dealing with just value objects
<FromGitter> <aboeglin> so the read method wouldn't take a struct
<FromGitter> <fusillicode_twitter> for example they can be merged :)
<FromGitter> <aboeglin> @fusillicode_twitter do you have a sample code to show merging of tuples ?
<FromGitter> <aboeglin> you mean like {key: value, **tuple} ?
<FromGitter> <fusillicode_twitter> @aboeglin https://play.crystal-lang.org/#/r/57pr :)
<FromGitter> <aboeglin> not sure if that is a valid syntax
<FromGitter> <j8r> @fusillicode_twitter because they are close under the ground, structs are generally more flexible and powerful
<FromGitter> <fusillicode_twitter> @j8r with more flexible & powerful you mean that they let you attach custom behavior right?
<FromGitter> <aboeglin> oh, I see, haven't checked the api of NamedTuple, great to know !
<FromGitter> <aboeglin> like a struct for instance wouldn't have that merge mechanism no ?
<FromGitter> <fusillicode_twitter> @aboeglin πŸ‘
<FromGitter> <j8r> @fusillicode_twitter it's one reason
<FromGitter> <fusillicode_twitter> @j8r πŸ‘
<FromGitter> <j8r> Also in Crystal, Symbols can be sometimes be avoided. There even talks about to remove them in the far future
<FromGitter> <drum445> So how do I pass a rs to that function @aboeglin
<FromGitter> <fusillicode_twitter> @j8r OMG really?!
<FromGitter> <aboeglin> @drum445 what do you mean ?
<FromGitter> <aboeglin> read is a method of ResultSet
<FromGitter> <drum445> it is, but rs.read only returns on column at a time doens't it?
<FromGitter> <drum445> not the entire row at once
<FromGitter> <aboeglin> not, that the whole point of the overloaded method I gave you
<FromGitter> <aboeglin> the thing is in the s : columnS
<FromGitter> <aboeglin> #Reads the next columns and returns a named tuple of the values.
<FromGitter> <drum445> maybe having a bad morning, but I can't see how that would be used to return a named tuple of an entire row
<FromGitter> <drum445> without having to declare the named tuple manually anyway
<FromGitter> <aboeglin> so if you do: ⏎ row.read({col : Type, otherCol : OtherType})
<FromGitter> <aboeglin> you'll get a named tuple with the values
<FromGitter> <aboeglin> oh you'd have to declare the types at some point
<FromGitter> <aboeglin> what you gain here is that you can read the whole row in one go
<FromGitter> <aboeglin> say you have a user table with email and id
<FromGitter> <aboeglin> you could have : ⏎ ⏎ ```alias UserRow = {email : String, id : Int32} ⏎ ... ⏎ ``` [https://gitter.im/crystal-lang/crystal?at=5bc07fcf82893a2f3bd00ecb]
<FromGitter> <drum445> yup true
<FromGitter> <drum445> not bad tbh, n1
<FromGitter> <aboeglin> you'd still need to define the types, it's a statically typed language, it's inherently there.
<FromGitter> <aboeglin> I haven
return0e has quit [Ping timeout: 252 seconds]
return0e has joined #crystal-lang
return0e has quit [Remote host closed the connection]
return0e has joined #crystal-lang
return0e has quit [Remote host closed the connection]
return0e has joined #crystal-lang
<FromGitter> <ljuti> How do I catch and test exceptions in macros?
dalpo has joined #crystal-lang
<FromGitter> <j8r> macros generate text @ljuti . you can debug with `{% debug %}`
<jokke> fussilicode_twitter: yah merging is a major selling point to me
<jokke> i love using named tuples for tests
<jokke> (as well)
<FromGitter> <fusillicode_twitter> truestory jokke :)
<FromGitter> <fusillicode_twitter> but I'm scared after what @j8r said about symbols XD
<FromGitter> <fusillicode_twitter> btw guys is there an "out-of-the-box" way to handle file upload with the stdlib `HTTP::Client `
<jokke> client.get("/foo/bar") { |response| { id: Int32 }.from_json(response.body_io.not_nil!, root: "data" }[:id].should eq(3)
<jokke> this of course can be put into a neat little macro
<FromGitter> <fusillicode_twitter> I know that `Crest` gives some facilities from this point of view (and not only) but right now I'm using the standard HTTP::Client and I don't know how much time it would cost me to switch to Crest :(
<jokke> you mean multipart uploads?
<jokke> sure
<jokke> sorry
<jokke> that ^
<jokke> but to be honest it's really hard to detect if something is a file or not in a general way without knowing anything about the request
<jokke> you could check if filename is set
<jokke> but i'm not sure if all clients do this
<FromGitter> <fusillicode_twitter> mmm
<FromGitter> <fusillicode_twitter> let me check one thing
<jokke> i'm currently writing a sort of file bin. like a pastebin where you can upload pretty much anything and i decided not to support multipart uploads and use raw binary bodies instead
<FromGitter> <fusillicode_twitter> well it makes sort of sense
<jokke> it will have some overhead on multi-file uploads but keep-alive will take care of most of that
<FromGitter> <fusillicode_twitter> got it...but...how will be handled the upload with just the raw binary body? πŸ€”
<jokke> you mean on client side?
<FromGitter> <fusillicode_twitter> yep
<jokke> not without js
<jokke> :/
<FromGitter> <j8r> my application segfault :(
<FromGitter> <fusillicode_twitter> my use case is to pick a file from the internet and send it to another api
<FromGitter> <j8r> Inside a Docker Alpine, I `crystal build --static --link-flags "-lcrypto -lz" src/dppm.cr` https://github.com/DFabric/dppm
<FromGitter> <fusillicode_twitter> :(
<FromGitter> <j8r> Compilation OK, then: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5bc09e4c435c2a518e94eb7f]
<FromGitter> <j8r> I have with gdb ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5bc09f2bc08b8b3067055a2b]
<FromGitter> <j8r> backtrace ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5bc09ff5c7bf7c3662fc08f3]
<FromGitter> <j8r> oh that's only when i require openssl
<FromGitter> <j8r> even removing openssl, still segfault :(
<FromGitter> <j8r> I've reduced the problem
DTZUZO has quit [Ping timeout: 252 seconds]
<FromGitter> <aboeglin> @j8r so what is the issue ?
Yxhuvud has quit [Remote host closed the connection]
return0e has quit [Read error: Connection reset by peer]
return0e has joined #crystal-lang
Yxhuvud has joined #crystal-lang
<FromGitter> <fusillicode_twitter> mmm
<FromGitter> <fusillicode_twitter> Crest or Halite?
<FromGitter> <fusillicode_twitter> πŸ€”
<FromGitter> <j8r> @fusillicode_twitter yes, personnaly after trying the both I prefer https://github.com/crystal-community/cossack
<FromGitter> <fusillicode_twitter> mmm
<FromGitter> <fusillicode_twitter> let me check
<FromGitter> <j8r> Halite is often a bit too overengineered, crest isn't as type safe because of the use of Symbols
<FromGitter> <fusillicode_twitter> wow these are really nice insights!
<FromGitter> <j8r> I use it like `response = Cossack::Client.new(&.use Cossack::RedirectionMiddleware).get url`
<FromGitter> <fusillicode_twitter> mmm
<FromGitter> <j8r> but you can use post, sure
<FromGitter> <fusillicode_twitter> well I would actually need to perform json posts and multipart form uploads together with logging
DTZUZO has joined #crystal-lang
<FromGitter> <fusillicode_twitter> to me it seems that both halite and crest were pretty good from the logging point of view but I didn't find anything explicitly documented regarding generating separate requests and responses objects
<FromGitter> <fusillicode_twitter> (I took a look inside Halite source and I saw the `Halite::Request` but the I didn't actually saw how it can be passed and performed to the client)
<FromGitter> <fusillicode_twitter> It seems that with Crossack I'll need to build a custom logger middleware :(
<FromGitter> <fusillicode_twitter> and moreover it doesn't have any hint about multipart upload :(
DTZUZO has quit [Quit: WeeChat 2.0]
<FromGitter> <j8r> it's more basic
<FromGitter> <fusillicode_twitter> got it
DTZUZO has joined #crystal-lang
<FromGitter> <j8r> crest and halite are good too, halite are loads of features
DTZUZO_ has joined #crystal-lang
DTZUZO has quit [Ping timeout: 252 seconds]
Heaven31415 has joined #crystal-lang
<Heaven31415> Hi
<Heaven31415> Is it okay to call #initialize on some object to recreate it?
<jhass> I wouldn't say that's a contract anybody really follows for it
<jhass> so it'd only work by luck
<RX14> it's likely to work for most objects
<RX14> given that #initialize assumes the class is garbage data
<jhass> mh do we actually move class level initializers to #initialize somehow?
<RX14> i think we do
<RX14> i'm 80% sure thats how its desugared
<RX14> given that .new really is just .allocate, #initialize then register the finalizer
<jhass> still, #allocate shouldn't be that expensive to be really worth to avoid
<RX14> yeah
<FromGitter> <fusillicode_twitter> @j8r I think that I've found what Halite is missing XD
<FromGitter> <fusillicode_twitter> i.e. the possibility to separately a request and let it be performed by an http client
<FromGitter> <fusillicode_twitter> Crest does have it though
non-aristotelian has joined #crystal-lang
<FromGitter> <fusillicode_twitter> but I'm really really sad that Halite is missing this fundamental feature :'(
<FromGitter> <bew> RX14 I noticed a few days ago that initialize is protected, and can't be called from the outside, it's very annoying when you want to initialize some garbage memory (need to manually create a method that calls initialize).
<RX14> you really shouldnt need to call that from the outside
<FromGitter> <bew> Don't you think it should be public for this kind of application?
<RX14> what kind of application
<FromGitter> <bew> But what if I actually need to? (i know, I'm doing some unsafe stuff, but the language shouldn't block me..)
<RX14> "i need to" isn't a usecase
<FromGitter> <bew> I was thinking about an ObjectPool, that pre-alloc a lot of classes (in one GC call), and initialize each one of them on demand
<RX14> thats called a GC
<RX14> except it handles multiple different class types
<RX14> so it's actually better
<FromGitter> <bew> Yes but no, isn't it slow when you alloc 10000 classes or so?
<FromGitter> <Blacksmoke16> if you have two modules that define a method with the same name, is there a way to pick which one is used if you include both
<RX14> order the include definitions
<FromGitter> <Blacksmoke16> oh, easy enough
<FromGitter> <Blacksmoke16> cheers
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/57v5 shouldnt this print `{"name": "bar"}` then since it should be using the `JSON::Serialization` method?
<RX14> no,
<RX14> firstly last wins
<RX14> secondly JSON::Serializable uses macros
<RX14> oh cool
<RX14> this does work
<FromGitter> <Blacksmoke16> yea, will use my `to_json` no matter the order
<RX14> oh sorry i misread
<RX14> yeah
<FromGitter> <bew> Looks weird, because `to_json` is defined on Object iirc, no?
<RX14> thats because you should be overriding to_json(builder)
<RX14> the to_json with no args is defined ob object
<RX14> so JSON::Serializable and your Serialize are defining different methods
<RX14> so neither will win, both will exist
<jhass> @bew what's slow is getting memory from the OS, so most if not all GCs get bigger chunks at a time and then assign parts of those as you request memory
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/57vb good call
<FromGitter> <Blacksmoke16> thanks!
<RX14> @Blacksmoke16 you can still do it with tuples
<RX14> just define to_json(builder) and use {"name2": @name}.to_json(builder)
<FromGitter> <Blacksmoke16> gotcha, makes it a bit cleaner
<FromGitter> <bew> That's right @jhass, and that's also part of why we use a GC, but I wanted to try to release the pressure on the GC for the specific case where you know you want a lot of instances of a specific data class (big or small)
<jhass> why not just set GC_INITIAL_HEAP_SIZE?
<Yxhuvud> bew: if you want to do hacks with allocation, it's allocate you'd want to do strange stuff in, not initialize. initialize is just like, take this memory area that was recently allocated (as far as it know), and make certain the data in it make sense.
<Yxhuvud> (or in `new`, I suppose)
<Yxhuvud> jhass: another reason to allocate in large chunks in advance can be to improve memory locality for the stuff you allocate
<Yxhuvud> which is dead easy for structs, but not as simple for objects
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/57y1 i made a prototype of a thing
<FromGitter> <j8r> hooo, rust has to type of Strings
<FromGitter> <j8r> to speak Crystal, one `class String` on the heap and one `struct String` on the stack
<FromGitter> <j8r> I thought `String` was a struct in crystal...
<FromGitter> <vladfaust> @Sija you've said that `Union(Int32 | String)` isn't idiomatic, but it's used in docs https://crystal-lang.org/api/0.26.1/Union.html
<jhass> it has used docs != it's used in the docs != it's idiomatic to use outside its purpose (macros)
<FromGitter> <j8r> yes that's the API reference, which is documented. That's different of being widely used
<FromGitter> <vladfaust> I don't get it
jsn- has joined #crystal-lang
<FromGitter> <j8r> documenting an object doesn't means its widely used, so idiomatic. The only thing we are sure is that's used in the compiler/stdlib
<FromGitter> <j8r> public API means also it may be useful for people
<FromGitter> <j8r> `grep -r 'Union(' | grep -v "union.*.cr" | wc -l` returns `29` times
<FromGitter> <j8r> mainly for yaml and json serialization
<FromGitter> <j8r> hummm half is used for specs, interesting! `Union(Float32, Array(Int32)).from_json(%(1.23)).should eq(1.23_f32)`
<FromGitter> <Sija> @vladfaust I take this as an example of flattening unions, not as idiomatic writing, in the end `Union` is defined with `*T` arguments meaning a splat - flattening is just a convenient shortcut
<FromGitter> <vladfaust> Okay, I see
<Heaven31415> Is it intentional that some C functions doesn't have bindings, like usleep and sleep? Probably because they are dangerous?
<jhass> In general you shouldn't need to do any direct LibC calls unless you're building an abstraction for those
<jhass> stdlib should have you covered for most usecases
<Heaven31415> Sure
<Heaven31415> Is there any chance that in the future compiler will handle this kind of situation? https://play.crystal-lang.org/#/r/57yy
<FromGitter> <j8r> to avoid to specify `@x : Int32`?
<Heaven31415> Exactly! I obviously don't know how compiler works, but from human perspective it looks very clear what type should be deduced, probably it's difficult to do because of how methods are instantiated
<FromGitter> <girng> ?
<FromGitter> <j8r> nope
<FromGitter> <j8r> `x = 0` set the type of `x` to `Int32`
<FromGitter> <girng> https://play.crystal-lang.org/#/r/57zo, looks like 0 defaults to Type Int32
<Heaven31415> Yeah, you can find it in book
<FromGitter> <bew> @Yxhuvud i'm not trying to do strange stuff in initialize, but outside of the target class (i want it to work on any class)
<FromGitter> <bew> Memory locality is a good idea too!
<Heaven31415> bew, do you mind creating some kind of prototype for ObjectPool in Crystal? I think I will need one.
<FromGitter> <bew> @Heaven31415 iirc it already works wheb the method is in a module/class (at module/class level)
<FromGitter> <bew> I have a working prototype of objectpool
<FromGitter> <bew> But it's a bit hacky and not polished
<FromGitter> <bew> (And relies on undocumented things...)
<Heaven31415> Sure, I won't judge :P
<FromGitter> <bew> Knowing this, here it is https://gist.github.com/bew/b816c76613574389445ba43b8f7110eb
<FromGitter> <bew> Use at your own risk :P
<FromGitter> <j8r> @Heaven31415 type restriction allow to faster compile time with less memory
<FromGitter> <bew> It works in a module: https://play.crystal-lang.org/#/r/5804 it should definitely work without it, looks like a bug
<FromGitter> <j8r> but i haven't find an issue/PR that talks about this this ;/
<FromGitter> <bew> You can open an issue if you want
<Heaven31415> Ok, I will open an issue about that.
<FromGitter> <bew> @j8r not really, the compiler will still type the whole method, to check that the return type is actually the type you indicated
<FromGitter> <j8r> I thought it was about object signature or something in this topic
<FromGitter> <bew> Ah yes sorry, forget what i said
druonysus has joined #crystal-lang
<FromGitter> <bew> Good! You can give the carcin links too (yours and mine)
<FromGitter> <j8r> @Heaven31415: you can also add `crystal` after first three ticks for syntax highlighting
<FromGitter> <bew> And you can add syntax highlighting by adding `cr` (or `crystal`) just after the 3 backticks (on the same line)
<Heaven31415> I wanted to ask about that, thanks guys
<Heaven31415> Ok, it's done, good job :P
<FromGitter> <ljuti> h
<FromGitter> <straight-shoota> @ljuti There is Object.delegate https://crystal-lang.org/api/0.26.1/Object.html#delegate(*methods,toobject)-macro
<FromGitter> <ljuti> @straight-shoota Thanks! It’ll probably be just what I need
DTZUZO_ has quit [Ping timeout: 252 seconds]
<FromGitter> <vladfaust> Just want to share some progress. I can say that Atom is almost ready. I still need to add docs, website and stuff to it, but CrystalJobs is working on Atom right now on my local machine, and it's working good! For sure, API will be updated to work with Jobs themselves until this Sunday, but front-end would take 1-3 days more... ⏎ ⏎ Take a look at the development branch and get amazed by amount of code
<FromGitter> ... reduced and overall flat structure! https://github.com/crystaljobs/api/tree/development
DTZUZO_ has joined #crystal-lang
pabs has joined #crystal-lang
<FromGitter> <aboeglin> Is there a syntax to declare a named instance property with its style in initialize ? ⏎ Something like this: ⏎ ´´´ ⏎ def initialize(name: @name : String) ⏎ ´´´ [https://gitter.im/crystal-lang/crystal?at=5bc12dc6271506518d0192ed]
<FromGitter> <spencerwi> apologies if this has been asked before, but is there a way to debug the specs in a library project?