shama has quit [Quit: (╯°□°)╯︵ɐɯɐɥs]
bcardiff has joined #crystal-lang
bcardiff has quit [Quit: Leaving.]
leafybasil has quit [Remote host closed the connection]
leafybasil has joined #crystal-lang
zamith has joined #crystal-lang
<zamith> hi, can anyone explain me how the IO object works?
<zamith> is it like a buffer, which gets flushed on prints?
zamith_ has joined #crystal-lang
zamith has quit [Ping timeout: 245 seconds]
travis-ci has joined #crystal-lang
<travis-ci> manastech/crystal#1799 (master - ef10476 : Ary Borenszweig): The build passed.
travis-ci has left #crystal-lang [#crystal-lang]
travis-ci has joined #crystal-lang
<travis-ci> manastech/crystal#1800 (master - 94eaa43 : Ary Borenszweig): The build passed.
travis-ci has left #crystal-lang [#crystal-lang]
bcardiff has joined #crystal-lang
canhtak has joined #crystal-lang
asterite has joined #crystal-lang
<zamith_> What's a DeclareVar?
zamith_ is now known as zamith
<asterite> When you write: x :: Int32
<asterite> Or @x :: Int32
<asterite> It’s mostly used in low-level code to define a static array without initializing it (for performance reasons): buffer :: UInt32[1024]
<zamith> I interested in particular in this line from the getter macro: {% name = name.var if name.is_a?(DeclareVar) %}
<asterite> But in a class or struct you can use it to force an instance variable to be of that type
<asterite> Oh, you can do: getter foo :: Int32
<zamith> oh ok
<zamith> got it
<zamith> thanks
<zamith> about what I've asked earlier, care to explain how the IO object works?
<zamith> is it like a buffer, that gets flushed on print, for instance
<zamith> ?
<asterite> Oh, I didn’t see it earlier, sorry
<asterite> In Crystal IO is a module that just provides convenience methods for reading and writing somewhere
<zamith> In particular, why does the to_s method have to receive an io?
<asterite> an object include IO must implement write(slice, count) and read(slice, count)
<asterite> We chose to do it in a different way than Ruby for efficiency
<asterite> for example, if you do `puts [1, 2, 3]`
<asterite> if puts just calls to_s on the argument, a String must be created from the Array
<asterite> then that string is printed, and it’s not used anymore
<asterite> Instead, if it’s to_s(io), the Array implementation writes “[“ to the give IO, then writes each of the values separated by a comma, then the ending “]”
<asterite> there’s no need to create intermediary strings
<asterite> And this is just for an Array. Imagine a big object containing other sub-objects, lots of strings would have to be created
<zamith> I understand
<asterite> We also use this in json generation, it’s to_json(io)
<asterite> In Ruby/Rails it’s worse because you first create a big hash/array and then convert that to a String, and then send it to the browser
<asterite> It’s very, very slow
<zamith> actually, I got into a funny behavior when I was implementing the inspect method for some objects which were in an array, and then leveraging the fact that the p method calls inspect, and in ruby it just printed what I've defined
<zamith> in crystal it added the [ and ], and the commas in between
<zamith> I found it was because of how to_s (which then inspect calls) is implemented in Array
<asterite> Oh, I think that’s because `puts array` in Ruby prints the element one by one separated by a newline, but I’m not sure I like that
<zamith> yeah
<zamith> I just wasn't expecting it
<asterite> For me it’s unexpected too :)
<zamith> thanks so much for the feedback
<asterite> :)
<zamith> asterite: How does lib find which C library we want to bind to?
<zamith> for example lib C
<zamith> fun atoi(str : UInt8*) : Int32
<zamith> how does it know what lib it is?
<asterite> Oh, libc is always linked
<asterite> so it doesn’t have a @[Link] attribute
<zamith> oh ok
<zamith> that's what I was missing
<zamith> thanks
<zamith> asterite: still regarding the IO object, when you say that it writes each of the values, you mean it writes them to bytes directly, without creating a string?
<asterite> Yes, you can do that
<asterite> For example Int#to_s does that: https://github.com/manastech/crystal/blob/master/src/int.cr#L181
<asterite> Well, it writes the bytes to an intermediary buffer (allocated on the stack) and then writes that buffer to the given IO
<zamith> sure
<asterite> (because if you write it directly you will get the number reversed)
<zamith> but that's the optimization you were talking about, right?
<zamith> or are there more benefits than those?
<asterite> No, it’s just that it’s more efficient
<asterite> I mean, the optimization is in every to_s(io) implementation, which doesn’t need to create an intermediary string
<zamith> asterite: Why do you use a Generic to implement expectation, such as class EqualExpectation(T)?
<zamith> also, sorry for being so annoying :p
<asterite> You are not annoying :)
<asterite> We use a generic because otherwise EqualException’s @value’s type would be a huge union of all the possible types you instantiate it with, and that would cause a huge dispatch to be made on every “==“ call
<asterite> but if it’s generic then @value’s type would be T (but there would be many EqualExceptation(T) types, but that doesn’t affect performance)
<asterite> But mostly, in the early days that didn’t compile, it crashed the compiler due to some bugs. Maybe now it works if it’s not generic and maybe it’s still efficient, but I don’t know
<zamith> ok, but the only instantiation of it I see is Spec::EqualExpectation.new value
<zamith> does it figure out T from value?
<asterite> Yes
<zamith> oh, cool
<asterite> In that same way you can do: Array.new(4, false)
<zamith> Array is a generic
<asterite> and that will create an Array(Bool) of length 4 because it figures it out from “false"
<zamith> ?
<asterite> Yes
<zamith> got it
<zamith> nice
<zamith> so, the main (only?) goal of generics is to get a type when they are instantiated, for performance (safety?) reasons?
<asterite> Oh, it’s not documented :(
<asterite> It’s because a class’s instance variable accumulates all the types you assign to it
<asterite> so if array weren’t generic, if you do this: `[1]; [“hello”][0] #=> Int32 | String`
<asterite> all arrays would have their types mixed
<asterite> I’m sure I wrote something about this somewhere, but I can’t find it
<zamith> yeah, it's not documented
<zamith> :S
<zamith> that's a good example, thanks
<zamith> is there a REPL for crystal?
<zamith> I was trying crystal eval from STDIN, but it doesn't seem to work very well
<asterite> This works for me: `echo puts 1 + 2 | crystal eval`
<asterite> There was a REPL but it didn’t work well
<asterite> it’s hard to do a REPL for a compiled language
<asterite> Rust is having issues with that too (and so D)
<zamith> yeah, i guess
<zamith> it's a nice thing though
<zamith> :)
<asterite> Yes, they are very useful! But we don’t know yet how to implement one. We have some ideas, but it’s a big project
<jhass> I think package manager and perhaps debugger are more important first :P
<asterite> Definitely!
<zamith> crystal deps is not that bad
<zamith> I found it to work without issues
<asterite> Yes, it’s simple but works :)
<asterite> but there’s no notion of versions yet
<zamith> I was trying to figure out how it works
<zamith> where is the code for getting code from github?
<zamith> does it support other ways to get code?
<zamith> like local paths or something like that?
<asterite> Not yet, only github for now
canhtak has quit [Quit: canhtak]
<asterite> `with … yield` is kind of like Ruby’s instance_eval, only it’s not instance eval
<asterite> Mmm… you yield, but inside the block methods will be looked-up first in the “with” object
<asterite> So; `def foo; with -1 yield; end; foo { abs } #=> 1`
<asterite> It’s useful for writing DSLs
<zamith> and if not found it looks up the ancestors chain?
<asterite> (but it has its bugs)
<asterite> If it’s not found normal lookup is done
<zamith> asterite: Do you have an example of how to bind a method to a call on a specific URL on an http server?
<zamith> like a router
<zamith> Or do I have to manually parse the request?
<zamith> thanks
<zamith> :)
<asterite> As most projects we start, they are unfinished because we find things to do in the language itself (features, bugs)
<asterite> but I think it works :)
<zamith> Is it possible to match a route as "/api/*"?
<asterite> Not yet
<zamith> ok, thanks
<asterite> What are you trying to do? (very curious) :)
canhtak has joined #crystal-lang
shama has joined #crystal-lang
canhtak has quit [Quit: canhtak]
leafybasil has quit [Remote host closed the connection]
<zamith> just some examples to show at the local meetup
zamith_ has joined #crystal-lang
zamith has quit [Ping timeout: 264 seconds]
leafybasil has joined #crystal-lang
canhtak has joined #crystal-lang
zamith_ has quit [Quit: Be back later ...]
rpag has joined #crystal-lang
zamith_ has joined #crystal-lang
<zamith_> asterite: Is it possible to define a method for a specific type on a generic?
<zamith_> let's say a to_s for Foo(T) if T is an Integer and different one if it is a Float
<asterite> Not right now
<asterite> Well, you can use is_a? but not on T… hmmm… let me check
zamith_ is now known as zamith
<zamith> just wondering
<asterite> That’s a way :)
<asterite> and the is_a? won’t even exist after compilation
<asterite> Is that what you need?
<zamith> yeah, that works
<zamith> thanks
<zamith> also, is there an example of the use of websockets?
<asterite> Hmm… let me search
<zamith> thanks
<asterite> I think you could test that from a browser, but I forgot how
<zamith> I'll try to figure it out
<zamith> ;)
<zamith> I'm trying to build crystal locally, to make some patches, is vagrant up enough?
<asterite> I’m not sure, I think there were some missing dependencies
<asterite> Hmm… at least to compile the compiler
<asterite> but try it and we can fix it if it doesn’t work
<zamith> sure
<zamith> thanks
<zamith> using the websockets
rpag has quit [Quit: Leaving]
asterite has quit [Ping timeout: 240 seconds]
bcardiff has quit [Quit: Leaving.]
bcardiff has joined #crystal-lang
<zamith> asterite: I was able to compile crystal with make, after brew install crystal --with-llvm
<zamith> but to run it I also had to brew install bdw-gc, which is not documented
<zamith> I don't think there is a place to submit patches to the docs... :s
shama has quit [Quit: (╯°□°)╯︵ɐɯɐɥs]
shama has joined #crystal-lang
<zamith> Anyone knows how can I print something to the logger, at let's say warn level?
canhtak has quit [Quit: canhtak]
zamith has quit [Quit: Be back later ...]
zamith has joined #crystal-lang
naps62 has joined #crystal-lang