<FromGitter>
<jwoertink> Where does that bit of code come from?
<FromGitter>
<S-YOU> string.cr
hightower3 has quit [Ping timeout: 240 seconds]
<FromGitter>
<jwoertink> So what you're saying is writing a string to a file uses up more memory than writing bytes because of the additional header?
wontruefree has joined #crystal-lang
<FromGitter>
<S-YOU> If you are building new string dynamically, may be. But If you are writing something already in memory, it should be same. ⏎ And you can always add new definitions for File#write for String argument.
<FromGitter>
<jwoertink> I'm writing XML to some files. Would it be better to take the XML string, convert that to Bytes, then write the Bytes?
<FromGitter>
<S-YOU> you can try .to_slice and writing the slice?
<FromGitter>
<jwoertink> Ah, yeah, I could try that
<FromGitter>
<jwoertink> Thanks for the explanation!
<FromGitter>
<jwoertink> I'm still not super clear on it, but it at least opens it up a bit more.
<FromGitter>
<bmulvihill> you can write the XML using the shovel operator `<<` which will internally call `write` I think
<FromGitter>
<bmulvihill> and convert the bytes for you
<FromGitter>
<jwoertink> Oh? I didn't know about that one. Right now I'm just using `File.write` since that lets me write a string directly.
<FromGitter>
<bmulvihill> all IO have it
<FromGitter>
<S-YOU> Yeah
<FromGitter>
<bmulvihill> It will take any object, so you can pass a string
<FromGitter>
<jwoertink> sweet
wontruefree has quit [Quit: bye]
<FromGitter>
<jwoertink> Thanks for the help!
jsn- has quit [Ping timeout: 240 seconds]
jsn- has joined #crystal-lang
duane has joined #crystal-lang
<FromGitter>
<jwoertink> Well, I'm not good at benchmarking, but it looks like `f << "testing\n"` is faster than `f.write("testing\n".to_slice)` or `File.write("test.txt", "testing\n")` in case anyone was interested
<FromGitter>
<phoffer> How do I define/access a C constant in a library I’m linking? The docs have a page about constants under C bindings but it appears to be defining Crystal constants inside the lib
<FromGitter>
<masukomi> i have a program that acts on piped input, but if there is no piped input it should just end. So far i have not found anyway to test if there's anything in ARGF that doesn't block if it's empty.... anyone know how to get around this?
<FromGitter>
<bmulvihill> @jwoertink You can also use file.puts to automatically write the newline for you
Yxhuvud has quit [Read error: Connection reset by peer]
Yxhuvud has joined #crystal-lang
<FromGitter>
<S-YOU> << is going through IO buffer, it make sense it is faster.
wontruefree has quit [Quit: bye]
sherjilozair has quit [Quit: Leaving...]
snsei has joined #crystal-lang
duane has quit [Ping timeout: 240 seconds]
<FromGitter>
<S-YOU> @masukomi, STDIN.tty? check might works for STDIN blocking thing.
snsei has quit [Remote host closed the connection]
snsei has joined #crystal-lang
intrepid has joined #crystal-lang
intrepid has quit [Ping timeout: 260 seconds]
rohitpaulk has joined #crystal-lang
<FromGitter>
<codenoid> , how to do multiple check in one `has_key?`
<FromGitter>
<codenoid> hi
<FromGitter>
<bew> @phoffer what is a C constant? if it's a macro you must copy manually the value to Crystal, you say I want this macro, because the shared library doesn't have macro informations embedded
snsei has quit [Remote host closed the connection]
<FromGitter>
<bew> you *can't say ...
<FromGitter>
<bew> @codenoid do you have an example? I'd say just do multiple `has_key?`
snsei has joined #crystal-lang
<FromGitter>
<codenoid> i wanted something like `has_key?(["key1", "key2"])`
<FromGitter>
<codenoid> fit with array
<FromGitter>
<bew> just do multiple call
<FromGitter>
<codenoid> but my screen width doesn't fit with multiple has_key? definition
<FromGitter>
<codenoid> no, okay, i will call has_key? twice
<FromGitter>
<bew> and if you have an array of keys you need to check you can do `keys_array.any? { |key| some_hash.has_key? key }`
<FromGitter>
<bew> or you can also use a tuple instead of `keys_array`
<FromGitter>
<codenoid> ok ,
snsei has quit [Remote host closed the connection]
<FromGitter>
<codenoid> tf, there is no error with `query.has_key?(["act", "user_id"])`
<FromGitter>
<codenoid> but it's not what i wanted
<FromGitter>
<bew> I agree it's quite hard to debug...
<FromGitter>
<bew> @asterite why not restrict the type of `has_key?`'s arg ?
<FromGitter>
<bew> hmm actually that won't work with union types
<FromGitter>
<bew> not sure
<FromGitter>
<bew> nvm, it should work
<FromGitter>
<bew> Oo `if (loc = location) && orig_loc = loc.original_location` => syntax error on the 2nd `=`
wontruefree has joined #crystal-lang
wontruefree has quit [Client Quit]
<FromGitter>
<bew> at it works when I add `()` around the second assignement
<FromGitter>
<bew> makes sense actually, but the error was weird at first
That_Guy_Anon has joined #crystal-lang
That_Guy_Anon has quit [Quit: Leaving]
alex`` has quit [Quit: WeeChat 2.1]
alex`` has joined #crystal-lang
salvor has quit [Ping timeout: 256 seconds]
tliff has quit [Ping timeout: 276 seconds]
asterite has quit [Ping timeout: 260 seconds]
jhass has quit [Ping timeout: 256 seconds]
DeBot has quit [Ping timeout: 256 seconds]
coderobe has quit [Ping timeout: 240 seconds]
literal has quit [Ping timeout: 276 seconds]
tilpner has quit [Ping timeout: 260 seconds]
coderobe has joined #crystal-lang
tilpner has joined #crystal-lang
salvor has joined #crystal-lang
Raimondi has quit [Remote host closed the connection]
Raimondi has joined #crystal-lang
<FromGitter>
<bararchy> When other lang talk about List, is it an Array or more like a Hash? (or Set?)
<FromGitter>
<bew> it depends on the language :P I'd say Array is the closest, but it could be a linked list
DeBot has joined #crystal-lang
<FromGitter>
<ArtLinkov> In python list is an array, and dictionary is a hash
asterite has joined #crystal-lang
<FromGitter>
<ArtLinkov> I think go has similar syntax
jhass has joined #crystal-lang
<FromGitter>
<bararchy> @bew Linked-Lists are just something we don't really need when not in C right? ⏎ Basiclly `[pointer, data] => [pointer, data] => [pointer,data]`, which we can have Array of class in Crystal
yopp has joined #crystal-lang
literal has joined #crystal-lang
_whitelogger has joined #crystal-lang
<badeball>
a linked list offers better performance for certain list-operations
<FromGitter>
<bararchy> Wouldnt a simple Iterator with next be enough to have the same capabilties without losing performance because of Dynamic array?
<FromGitter>
<yxhuvud> @bararchy there is a few places in stdlib where a linked list is used, but most of the time we don't need it. There are some cases when it is necessary to delete in the middle of lists where it makes sense to use them instead of arrays
<FromGitter>
<bararchy> @yxhuvud Where is it used? sounds interesting
<FromGitter>
<yxhuvud> it is not extracted to a separate class, but it is part of scheduler and Hash implementations.
<FromGitter>
<yxhuvud> and potentially some other places
Nik736 has joined #crystal-lang
Nik736 has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
Nik736 has joined #crystal-lang
<wuehlmaus>
anybody using the qt library of Papierkorb on a Mac [important prerequisite]. I am wondering because it would be my preferred library and i am getting fundamental require problems that i don't understand, shards install worked without error but crystal spec did not. Sadly Papierkorb is not there.
<crystal-gh>
[crystal] asterite opened pull request #6127: Fix incorrect formatter of ending expression after heredoc (master...bug/6121-formatter-heredoc) https://git.io/vhIJX
ua has quit [Read error: Connection reset by peer]
ua has joined #crystal-lang
Nik736 has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
That_Guy_Anon has joined #crystal-lang
duane has quit [Ping timeout: 276 seconds]
<crystal-gh>
[crystal] asterite opened pull request #6128: Fix: detect error when trying to use unbound generic type in instance var (master...bug/5383-unbound-generic-type) https://git.io/vhI3p
Nik736 has joined #crystal-lang
Nik736 has quit [Client Quit]
Nik736 has joined #crystal-lang
wontruefree has joined #crystal-lang
Nik736 has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<FromGitter>
<asterite> @paulcsmith you'll have to change assert_error to pass a filename and pass it along to other methods. In spec_helper there's an optional filename argument in some of the methods
wontruefree has quit [Quit: bye]
shalmezad has quit [Quit: This computer has gone to sleep]
rohitpaulk has joined #crystal-lang
shalmezad has joined #crystal-lang
wontruefree has joined #crystal-lang
Nik736 has joined #crystal-lang
<FromGitter>
<jwoertink> When you have a method that uses `with Something.new yield`, in the block `self` doesn't refer to `Something`. Is there a way to get that context other than just passing it in the block?
That_Guy_Anon has quit [Remote host closed the connection]
<RX14>
according to the latter error message (which was my understanding of the ivars type inference rules) that inferece would be impossible because we had to type Gen.new with it
<FromGitter>
<jwoertink> nice! Thanks @bew :D `itself` is what I needed
<RX14>
and i thought the whole point was not to type methods in the ivars pass
<FromGitter>
<paulcsmith> @asterite Ok I'll take a look in spec_helper and see if I can cobble something together
<FromGitter>
<asterite> RX14 type inference for instance vars work with class methods, but never with instance methods, and only if they have an explicit return type or if the type can be inferred from the body
<RX14>
but what if the class method instantiates an instaqnce method?
<FromGitter>
<asterite> It's not hard to understand. The compiler won't deal with instance methods at that point
<FromGitter>
<asterite> If you want an improvement, let's force typing instance vars, always, like any other language
<FromGitter>
<asterite> That's consistent, easy to understand, fast to compute
<RX14>
I thought that thats what we *did*
<FromGitter>
<asterite> It seems like everyone is both enjoying type inference and hating it at the same time :-P
<RX14>
those few small inference rules seemed to me automatic and basic
<RX14>
not invoking the real typing algorithm at all
<RX14>
just looking up literals and return type annotations
<FromGitter>
<asterite> Right, it's never invoked
<RX14>
if we don't invoke the real typing algorithm my question is how we type class method bodies?
<FromGitter>
<bew> `type inference for instance vars work with class methods` what's the reason behind this?
rohitpaulk has quit [Ping timeout: 245 seconds]
<RX14>
do they have a weird seperate different type inference algorithm?
<RX14>
or is it just a specific part of the typing algorithm that deals with ivars we avoid
<FromGitter>
<asterite> I mean, if you have Foo.new that infers to Foo
<RX14>
but I can do Foo.foo
<RX14>
where self.foo doesn't have a return type annotation
<RX14>
and it's body contains a consitional
<FromGitter>
<asterite> But what if someone over wrote that new? That's why this rule exists, it tries to see if the method exists and returns something else
<FromGitter>
<asterite> Conditional is easy: union of both branches
<RX14>
so the "type cannot be inferred" message isn't correct
<FromGitter>
<asterite> Well, PRs are welcome if someone wants to remove all those things
<RX14>
we can infer Type.method where Type.method doesn't have a return annotation
<RX14>
i'd be interested to see how much it breaks...
<FromGitter>
<asterite> Except we can if the body is a literal
<FromGitter>
<asterite> Probably doesn't break any code. It was simple to implement so I did it
<FromGitter>
<jwoertink> What's your go to method signature when doing optional arguments? Just using a Hash? or doing named arguments and just have a method with possibly 12 arguments?
<FromGitter>
<bararchy> @jwoertink Well, it really depends on the usecase, usually when I find my self passing too many arg I just figureout if I can pass on an Object that has those vars inside
<FromGitter>
<jwoertink> hmm... Like making a Struct just for additional options?
<FromGitter>
<bararchy> yeha
<FromGitter>
<jwoertink> that might not be a bad idea. I'm currently using NamedTuple and it's a nightmare so I need to refactor
<FromGitter>
<jwoertink> I wanted something that looked like this `add("/about", changefreq: "yearly", priority: 0.1)`
<FromGitter>
<jwoertink> but how I have that method defined, doing this fails
<FromGitter>
<bew> how is it defined?
<FromGitter>
<jwoertink> I guess I could do `def add(path : String, options : Options)` and make `Options` a struct
<FromGitter>
<jwoertink> it's actually defined right now as `add(path : String, options : Options)`
<FromGitter>
<bararchy> @jwoertink Not sure why but Crystal urges you to use long Hash and Tuples to store data and very quicly it becomes ⏎ ⏎ ```Tuple(a: String, b: Hash | String, c: What)``` ⏎ ⏎ My golden rule is this, take 5 min break, if you come back and see the Type and it looks like a huge monster, just make a class\struct [https://gitter.im/crystal-lang/crystal?at=5b06fec3752aff593110e0c1]
<FromGitter>
<jwoertink> but Options is an alias to a NamedTuple
<FromGitter>
<jwoertink> @bararchy that's a good point
<FromGitter>
<bararchy> @girng Can also tell you about it happening to him :)
<FromGitter>
<Grabli66> How to store any object in Hash? Want like Hash(String, AnyObject)
<FromGitter>
<noahlh> my_method doesn't need to handle the nil type. I think I understand why but I don't fully grok it. Can someone explain?
greenbigfrog has quit [Ping timeout: 255 seconds]
sz0 has joined #crystal-lang
marrow16180 has quit [Quit: Leaving]
greenbigfrog has joined #crystal-lang
<FromGitter>
<bew> I bet `foo` is a method, right?
<FromGitter>
<bew> @noahlh
<FromGitter>
<noahlh> in the case where I discovered this it's an instance variable
<FromGitter>
<noahlh> (which gets its value from a method)
<FromGitter>
<bew> yeah it's the same logic but for a method it's simpler to understand: what if the method returned something else the second time you call it? the compiler can't be sure that the value returned by the method will be the same, and could be `nil`
<FromGitter>
<bew> it's the same with instance variables, between the 2 access, the ivar could have changed (from another fiber for example)
<FromGitter>
<bew> so the solution is to use a local variable to store the value locally, so that if the method/ivar changes, you'll still have the old value in the local var, that you can use