jhass changed the topic of #crystal-lang to: The Crystal programming language | http://crystal-lang.org | Crystal 0.7.4 | Paste > 3 lines of text to https://gist.github.com | GH: https://github.com/manastech/crystal - Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/ | Logs: http://irclog.whitequark.org/crystal-lang
waj has quit [Client Quit]
grindhold has joined #crystal-lang
<sardaukar> so, I have a bunch of thing in an array, and as I go through them I want to call a method based on a key of the element in the array
<sardaukar> how can I do this without a huge switch ?
<sardaukar> or is the switch the best way?
<jhass> the switch is pretty much the best way
<jhass> you might be able to automate its generation to some degree with the macro language
<sardaukar> yeps
<jhass> on the plus side it's fast
<sardaukar> I thought this language was happiness-oriented too. switches are boring :D
<jhass> well, switchless variant would require a runtime
<jhass> = slow/bloat
<sardaukar> yeah
<jhass> and this language is pretty much performance oriented too :P
<sardaukar> it's insanely fast alreadty
Locke23rus has quit [Remote host closed the connection]
shama has quit [Remote host closed the connection]
<sardaukar> man, I still don't have the hang of types in Crystal
<sardaukar> what am I missing?
<sardaukar> I'm only using an int to index the array, why does Crystal assume I want to use the rest of the types present on the box object?
<jhass> because you pass it as a hash, the hash becomes a Hash(Symbol, Int|Symbol|String|Style)
<jhass> and thus all values you fetch from it do
<sardaukar> where do I pass it as a hash?
<jhass> Widget.new
<sardaukar> I just do @buffer[box.y ]
<sardaukar> isn't that an Int ?
<jhass> at runtime, yes
<sardaukar> at compile time, too :D
<jhass> but the compiler can't track hash keys
<sardaukar> even if I assign it to an int, it won't work
<jhass> so typeof(@opts[:x]) is Int32|Symbol|String|Style
<jhass> and thus typeof(@x) is
<jhass> and thus typeof(widget.x) is
<jhass> same for all others
<sardaukar> how can I fix it?
<jhass> don't use an opts hash
<sardaukar> add an accessor for all those types to the buffer array?
<sardaukar> oh
<sardaukar> but... that sucks
<sardaukar> will that ever change?
<jhass> nope
<sardaukar> but... but...
<jhass> and well, we're back to required keyword args
<sardaukar> but if the compiler is not this smart, a lot of people will complain
<sardaukar> just like me :D
shama_ has joined #crystal-lang
<jhass> @hash[[:a, :b].sample] = [1, 1.0].sample
<jhass> now what?
<sardaukar> left side is always a Symbol (sample on that array will always return one) and right side is Int32|Float
<sardaukar> right?
<jhass> right
<jhass> case in point is rather, just because it's obvious to you that in a particular case a certain key will always have a value of a certain type, that doesn't make it obvious to the compiler at all
<jhass> it needs to handle the general thing, which is ^
<sardaukar> I'm sure I'm being daft, but I don't see why in this case
<jhass> for starters Hash is just written in Crystal, there's nothing special about it
<jhass> you could recreate it 100% without any special compiler support
<sardaukar> sure
<sardaukar> I really dig that about the stdlib
<jhass> so #[]= is just an arbitrary method call doing arbitrary things
<sardaukar> still with you
bcardiff has quit [Quit: bcardiff]
<jhass> you're asking that crystal figures that after foo.bar(:a, 1); foo.bar(:b, "a"), foo.baz(:a) will return Int32 instead of Int32|String or whatever
<jhass> or even for foo.bar(1, :a); foo.bar("a", :b) since argument order is again just arbitrary
<sardaukar> yes - if you've set up foo to be Symbol -> Int32|String
<sardaukar> isn't that the reason for declaring the hash's types?
<jhass> foo is an instance of class Foo(T, U)
<jhass> no, it's so you know which types to take as key and which types to take as values
<jhass> and generics make it so that each instance can get their own set of types instead of them being union across all instances
<jhass> but that doesn't make some values suddenly define some other values type
<sardaukar> in my mind, when I say Symbol => Int32|String, it means I can do h[[:a, :b].sample] = [1,"a"].sample
<sardaukar> right?
<jhass> yes
<jhass> but you can't expect h[:a] #=> String just because you did h[:a] = "foo"
<sardaukar> I don't understand the generics bit too much
<sardaukar> right, I get that
<sardaukar> it should be Int32|String
<jhass> >> class Foo; getter bar; def initialize(@bar); end; end; Foo.new(1); typeof(Foo.new("foo").bar)
<DeBot> jhass: # => (String | Int32) - http://carc.in/#/r/7v9
bcardiff has joined #crystal-lang
<jhass> >> class Foo(T); getter bar; def initialize(@bar : T); end; end; Foo.new(1); typeof(Foo.new("foo").bar)
<DeBot> jhass: # => String - http://carc.in/#/r/7va
<sardaukar> I see
<sardaukar> so the type changes over time?
<sardaukar> if you hadn't done Foo.new("bar") it would just be Int32
<jhass> yes
<sardaukar> because the class lingers, and all instances must be valid
<sardaukar> got it
<sardaukar> generics puts up walls around the instances
<sardaukar> so they have only the specified type
<jhass> I like to thing about them sort of like ondemand subclasses adding type restrictions
shama_ has quit [Remote host closed the connection]
<sardaukar> YES
<sardaukar> I GET IT
<sardaukar> THANK YOU
<sardaukar> big epiphany here
<sardaukar> cheers
<sardaukar> couldn't I use this to fix the box.y issue as an array index?
<jhass> not really, just get rid of the options hash and make the parameters and crystal will figure it out for you
<jhass> *them
<sardaukar> yeah, already did it
<sardaukar> couldn't I do y: T on the initializer?
<sardaukar> on some of the arguments?
<jhass> you could, not sure what you'd gain from it though
<sardaukar> wouldn't that "box" y to an Int32 ?
<jhass> no
<jhass> it would prevent y from becoming a union if you were to pass different types to it
NeverDie has joined #crystal-lang
<sardaukar> and wouldn't that allow Crystal to know that y was an int?
<jhass> no, if you'd want to enforce that just do it, y : Int32
<sardaukar> isn't that the same as y: T and passing it an int?
<jhass> no, : T still allows me to pass a String too
<sardaukar> yes, ok
<jhass> and on the outside you'd just shift your union from String|Int32 on the y to Widget(String)|Widget(Int32) on the array
<sardaukar> I have much to learn
<sardaukar> thanks for all your help!
<sardaukar> kinda late in London now :)
<sardaukar> c ya!
<jhass> good night!
bcardiff has quit [Quit: bcardiff]
willl has quit [Quit: Connection closed for inactivity]
shama has joined #crystal-lang
shama has quit [Client Quit]
havenwood has joined #crystal-lang
sailorswift has quit [Quit: My Mac has gone to sleep. ZZZzzz…]
ozra has quit [Ping timeout: 246 seconds]
bcardiff has joined #crystal-lang
strcmp1 has joined #crystal-lang
BlaXpirit has joined #crystal-lang
bcardiff has quit [Quit: bcardiff]
sandelius has joined #crystal-lang
sandelius has quit [Quit: Textual IRC Client: www.textualapp.com]
BlaXpirit has quit [Quit: Konversation]
NeverDie has quit [Ping timeout: 244 seconds]
NeverDie has joined #crystal-lang
dbackeus has joined #crystal-lang
Ven has joined #crystal-lang
havenwood has quit [Ping timeout: 240 seconds]
sailorswift has joined #crystal-lang
NeverDie has quit [Quit: I'm off to sleep. ZZZzzz…]
BlaXpirit has joined #crystal-lang
Ven has quit [Ping timeout: 246 seconds]
sailorswift has quit [Quit: My Mac has gone to sleep. ZZZzzz…]
sailorswift has joined #crystal-lang
dbackeus has quit [Remote host closed the connection]
<crystal-gh> [crystal] jbbarth opened pull request #1025: Yield start and finish indices in Readline.autocomplete (master...enhancement/readline-autocomplete-yield-start-finish) http://git.io/vYTND
dbackeus has joined #crystal-lang
dbackeus has quit [Remote host closed the connection]
dbackeus has joined #crystal-lang
Ven has joined #crystal-lang
Ven has quit [Read error: Connection reset by peer]
sailorswift has quit [Quit: My Mac has gone to sleep. ZZZzzz…]
Ven has joined #crystal-lang
Ven has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
sandelius has joined #crystal-lang
Ven has joined #crystal-lang
ponga has joined #crystal-lang
<crystal-gh> [crystal] asterite pushed 2 new commits to master: http://git.io/vYk1t
<crystal-gh> crystal/master eff4862 Jean-Baptiste Barth: Fix Regex 'm' modifier mistakenly mapped to PCRE_DOTALL
<crystal-gh> crystal/master 4c88b8b Ary Borenszweig: Merge pull request #1020 from jbbarth/bugfix/regex-multiple-newlines...
<dbackeus> is it possible to define a "define_method" macro which actually takes a block and runs that when the method is called?
<jhass> sort of, you can call {{yield}} in a macro which will be replaced by the block passed to it
bcardiff has joined #crystal-lang
<dbackeus> can the arguments to a macro be dynamic? I seem to be able to do define_method("foo") but not define_method("FOO".downcase) nor define_method(my_string_var) for example.
<jhass> no they can't, keep in mind that macros are evaluated at compile time
Liothen has quit [Ping timeout: 244 seconds]
Ven has quit [Ping timeout: 246 seconds]
bcardiff has quit [Quit: bcardiff]
Liothen has joined #crystal-lang
Ven has joined #crystal-lang
<dbackeus> right, thanks for the help
<dbackeus> slowly wrapping my head around compile time vs runtime :)
Ven has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
waj has joined #crystal-lang
Ven has joined #crystal-lang
<dzv> is there a preferred way to turn an Array(UInt8) to a string?
<jhass> >> String.new [87u8, 32u8, 78u8].buffer
<DeBot> jhass: # => "W N" - http://carc.in/#/r/7wt
<jhass> not sure I'd call it preferred, certainly the most efficient one
willl has joined #crystal-lang
ozra has joined #crystal-lang
<dzv> when working with binary data i should use Array(UInt8)?
bcardiff has joined #crystal-lang
<jhass> depends a bit, Slice might be a good tool too
bcardiff has quit [Client Quit]
<jhass> or StaticArray
<dzv> i'm dealing with io and merging buffers. slice doesn't have a + method
<jhass> could be an interesting addition though
<dzv> also resizing buffers. both growing and shrinking. slice doesn't really cut it (pun intended)
strcmp1 has quit [Ping timeout: 250 seconds]
<jhass> yeah, but I think it would make sense to make it the tool for all that
<asterite> dzv: are you writing to an IO?
<dzv> not always
<dzv> i have a c library which needs a buffer to write to. it also writes the length in an int of how much data there was
<dzv> this is combined with both read and write io often
<dzv> but not always
waj has quit [Ping timeout: 272 seconds]
waj has joined #crystal-lang
mgarciaisaia has joined #crystal-lang
<dzv> would it be helpful to have a Bytes alias for Array(UInt8)?
<jhass> I'm not sure
<jhass> as said I think Slice and StaticArray can be as valid tools
<jhass> even Pointer in some cases
<dzv> StaticArray doesn't really work. i need to allocate x bytes, call a c function and shrink it to the return size of the function. it often shrinks by 0-16 bytes. and the code is often in tight loops and performance sensitive
havenwood has joined #crystal-lang
<jhass> do you have a maximum? Then I'd probably go with a Pointer buffer of that size and place views on it with Slice's
<asterite> dzv: if you need a resizable pointer, then array is the solution. But I'd like to see some code to understand better your problem
mgarciaisaia1 has joined #crystal-lang
<dbackeus> if a MatchData contains a nil match it appears to make the whole object behave like nil: http://play.crystal-lang.org/#/r/7xn
<dbackeus> is this a bug or am I missing something?
<asterite> The nil-related thing will improve in the next version, it will tell you that match is actually MatchData | Nil
mgarciaisaia has quit [Ping timeout: 260 seconds]
<jhass> dbackeus: match returns nil if there's no match
<dbackeus> but there is a match, it's just the group that's nil
<dbackeus> it should only be match[1] which returns nil
<asterite> dzv consider this: http://play.crystal-lang.org/#/r/7xp
<jhass> dbackeus: there is, but the compiler doesn't know, so it's possible that there isn't
<asterite> Better use typeof, which gives you the compile-time type. "class" gives you the runtime type
<dbackeus> so in future versions could I expect this to not blow up? http://play.crystal-lang.org/#/r/7yh
ponga has quit []
<jhass> no, use match[1]? if you want nil
ozra has quit [Ping timeout: 246 seconds]
<dbackeus> aha, thanks!
mgarciaisaia has joined #crystal-lang
sandelius has quit [Quit: Textual IRC Client: www.textualapp.com]
mgarciaisaia1 has quit [Ping timeout: 244 seconds]
ponga has joined #crystal-lang
mgarciaisaia1 has joined #crystal-lang
shama has joined #crystal-lang
mgarciaisaia has quit [Ping timeout: 240 seconds]
waj has quit [Quit: waj]
dbackeus has quit [Remote host closed the connection]
dom96 has quit [Ping timeout: 246 seconds]
jbye has quit [Ping timeout: 255 seconds]
jbye has joined #crystal-lang
Ven has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
mgarciaisaia1 has quit [Quit: Leaving.]
dbackeus has joined #crystal-lang
dom96 has joined #crystal-lang
dbackeus has quit [Remote host closed the connection]
Ven has joined #crystal-lang
Ven has quit [Client Quit]
strcmp1 has joined #crystal-lang
willl has quit [Quit: Connection closed for inactivity]
<crystal-gh> [crystal] ysbaddaden opened pull request #1027: Expose Def#visibility to macros (master...macro-def-visibility) http://git.io/vYLRl
Liothen has joined #crystal-lang
Liothen has quit [Changing host]
mgarciaisaia has joined #crystal-lang
waj has joined #crystal-lang
dbackeus has joined #crystal-lang
dbackeus has quit [Ping timeout: 256 seconds]
dbackeus has joined #crystal-lang
NeverDie has joined #crystal-lang
mgarciaisaia1 has joined #crystal-lang
mgarciaisaia has quit [Ping timeout: 246 seconds]
mgarciaisaia1 has quit [Ping timeout: 256 seconds]
nahtnam has joined #crystal-lang
mgarciaisaia has joined #crystal-lang
sergey-kucher_ has joined #crystal-lang
NeverDie has quit [Quit: I'm off to sleep. ZZZzzz…]
BlaXpirit has quit [Read error: Connection reset by peer]
BlaXpirit has joined #crystal-lang
sailorswift has joined #crystal-lang
zipR4ND has joined #crystal-lang
sergey-kucher_ has quit [Ping timeout: 246 seconds]
jtarchie has joined #crystal-lang
sailorswift has quit [Read error: Connection reset by peer]
sailorswift has joined #crystal-lang
zipR4ND has quit [Ping timeout: 265 seconds]
<dbackeus> how come this works: http://play.crystal-lang.org/#/r/7yy
<dbackeus> but this doesn't: http://play.crystal-lang.org/#/r/7yw
<jhass> dbackeus: I guess it's something to do with hitting the gsub(String, String) overload
<jhass> which doesn't yield a matchdata
<jhass> so m is nil
<dbackeus> so the difference in your version is that a tuple tells the compiler the order of the types so it can be sure [0] is a regex?
<jhass> yeah
<jhass> Tuple is one of the few in the compiler special cased types
<dbackeus> thanks a lot
<dbackeus> another random question, how come .flatten hasn't been implemented on Array?
<jhass> because it's actually pretty tricky
lucasb has joined #crystal-lang
<lucasb> Hello. Newbie here. I guess this has already got asked lots of times, but again: I'm on x86-32bits, on github there are only binaries for 64bits, how can I compile crystal then?
<jhass> lucasb: you have to cross compile on a 64bit host. Maybe we can get you a binary to bootstrap, what's your OS?
<lucasb> jhass: Linux
mgarciaisaia has quit [Quit: Leaving.]
mgarciaisaia has joined #crystal-lang
mgarciaisaia has quit [Client Quit]
nahtnam has quit [Quit: Connection closed for inactivity]
mgarciaisaia has joined #crystal-lang
<jhass> lucasb: sorry, got carried away, which distro?
<lucasb> jhass: I'm on slackware, but I guess a binary compiled in any other recent distro would work just fine, wouldn't it?
<jhass> not really, got a couple dependencies that need to match up
<jhass> slackware got pretty recent versions? comparable to arch?
<lucasb> jhass: yeah, I think slackware has recent versions of tools/libs
<jhass> extract it somewhere, temporarily add that to your $PATH and run make in the repo
<lucasb> jhass: Awesome, downloading now! Thank you very much, jhass. I'll try later on.
BlaXpirit has quit [Quit: Konversation]
<jhass> if the sonames don't match you can try creating a dir with symlinks with matching ones and adding that to LD_LIBRARY_PATH
<lucasb> ok
zipR4ND has joined #crystal-lang
waj has quit [Quit: waj]
sailorswift has quit [Quit: My Mac has gone to sleep. ZZZzzz…]
waj has joined #crystal-lang
sailorswift has joined #crystal-lang
NeverDie has joined #crystal-lang
dbackeus has quit [Remote host closed the connection]
<lucasb> I'm hunt down some missing dependencies here my distro... first was libedit, then libpcl, now libunwind. I guess this is gonna take a while...
waj has quit [Quit: waj]
<jhass> that's for running the specs ultimately, but can't hurt to have it
<lucasb> jhass: Thanks. Yeah, I guess I'll have to install all these before that binary works
havenwood has quit [Read error: Connection reset by peer]
lucasb has left #crystal-lang [#crystal-lang]
havenwood has joined #crystal-lang
havenwood has quit [Read error: Connection reset by peer]
sailorswift has quit [Quit: My Mac has gone to sleep. ZZZzzz…]
zipR4ND has quit [Read error: Connection reset by peer]
mgarciaisaia has quit [Quit: Leaving.]
zipR4ND has joined #crystal-lang
mgarciaisaia has joined #crystal-lang
mgarciaisaia has quit [Ping timeout: 272 seconds]
mgarciaisaia has joined #crystal-lang
zipR4ND has quit [Ping timeout: 256 seconds]
mgarciaisaia has quit [Ping timeout: 246 seconds]
mgarciaisaia has joined #crystal-lang
dfockler has joined #crystal-lang
<dfockler> I'm able to run my code, but I can't build the code into an executable, getting an LD errno 21
mgarciaisaia has quit [Ping timeout: 244 seconds]
mgarciaisaia has joined #crystal-lang