<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
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
<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>
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
<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...