ChanServ changed the topic of #crystal-lang to: The Crystal programming language | | Fund Crystal's development: | GH: | Docs: | Gitter:
greenbigfrog has quit [Ping timeout: 272 seconds]
greenbigfrog has joined #crystal-lang
ur5us has quit [Ping timeout: 272 seconds]
ur5us has joined #crystal-lang
ur5us has quit [Ping timeout: 272 seconds]
ur5us has joined #crystal-lang
ur5us has quit [Ping timeout: 260 seconds]
taupiqueur has joined #crystal-lang
taupiqueur has quit [Remote host closed the connection]
taupiqueur has joined #crystal-lang
taupiqueur has quit [Remote host closed the connection]
taupiqueur has joined #crystal-lang
hightower2 has joined #crystal-lang
Sankalp has quit [Ping timeout: 240 seconds]
Sankalp has joined #crystal-lang
jmdaemon has quit [Ping timeout: 240 seconds]
wolfshappen has joined #crystal-lang
taupiqueur has quit [Remote host closed the connection]
walez has joined #crystal-lang
<FromGitter> <mattrberry> Is there a way to specify that a method returns any number type?
<FromGitter> <mattrberry> Without enumerating Int8 | Int16 | Int32 | etc
<FromGitter> <Blacksmoke16> yes
<FromGitter> <Blacksmoke16> `: Int::Primitive`
<FromGitter> <mattrberry> Thanks
<FromGitter> <Blacksmoke16> wait
<FromGitter> <Blacksmoke16> just use `: Int`
<FromGitter> <Blacksmoke16> or even `: Number`
<FromGitter> <Blacksmoke16> if you want to also allow floats
<FromGitter> <Blacksmoke16> cant use these to type an ivar, but as a return/param restriction is fine
_ht has joined #crystal-lang
walez has quit [Ping timeout: 240 seconds]
Sankalp has quit [Ping timeout: 252 seconds]
Sankalp has joined #crystal-lang
walez has joined #crystal-lang
walez has quit [Quit: Leaving]
hightower2 has quit [Remote host closed the connection]
_ht has quit [Quit: _ht]
ur5us has joined #crystal-lang
jmdaemon has joined #crystal-lang
<FromGitter> <plambert> `def foo(x : Array(String | Int32 | Nil)); puts "hello"; end; foo ["hello"]`
<FromGitter> <plambert> I'm doing something like that, and I get that my Array(String) that I'm passing doesn't match...
<FromGitter> <plambert> How can I make my array match the type that foo() is expecting?
<FromGitter> <plambert> I've tried `.as(Array(String|Int32|Nil))` but it says it can't case Array(String) to Array(String|Int32|Nil). I can see not being able to go the other direction, but an Array(String) is 100% guaranteed to work as an Array(String | Int32 | Nil)… why can't the compiler see that?
<FromGitter> <plambert> My example I gave (the def foo…) works in crystal eval… I'll try to reproduce my problem in something short…
<FromGitter> <Blacksmoke16> prob want to do like
<FromGitter> <Blacksmoke16> `foo ["hello"] of String | Int32 | Nil`
<FromGitter> <Blacksmoke16> the problem ultimately is an `Array(String)` isnt the same as `Array(String | Int32 | Nil)` as like what would happen if you did like `x << 10; x << nil`
<FromGitter> <plambert> So every caller has to explicitly cast to the exact type?
<FromGitter> <plambert> I've narrowed it to this: `table=Array(Array(String | Int32 | Nil)).new; table << ["hello", 27]`
<FromGitter> <Blacksmoke16> makes sense yea, the array you're trying to push doesnt allow nil
<FromGitter> <plambert> But it is a valid Array(String | Int32 | Nil)
<FromGitter> <Blacksmoke16> could make it an alias then would be able to do like `MyType["hello", 27]`
<FromGitter> <plambert> It is an alias… I could try that. Still ugly for the caller.
<FromGitter> <Blacksmoke16> but its not
<FromGitter> <Blacksmoke16> are trying to push an array that doesnt allow `nil` into an array that requires arrays of `String | Int32 | Nil`
<FromGitter> <plambert> It's a literal.
<FromGitter> <Blacksmoke16> right, so its type is assumed based on the values in the array, which is `Array(String, Int32)`
<FromGitter> <plambert> So, in my case, I'm passing the array to a method explicitly only accepting an Array(String | Int32 | Nil).
<FromGitter> <plambert> The method accepts it, but it remains an Array(String | Int32)
<FromGitter> <plambert> That's a reproduction of what I'm seeing.
<FromGitter> <plambert> I can't append the array to the existing array, even though I'm only accepting Array(TheTypes)... shouldn't the compiler be complaining that I'm passing Array(String|Int32) to #foo, which requires Array(String|Int32|Nil)?
<FromGitter> <Blacksmoke16> ah so there's ivars involved
<FromGitter> <Blacksmoke16> i think we're both right. compiler allows your array into the method, but is failing on the mutation of your ivar array
<FromGitter> <Blacksmoke16> sec
<FromGitter> <plambert> And the strangest thing is this: class Foo ⏎ alias TheTypes = String | Int32 | Nil ⏎ ⏎ def initialize ⏎ ... []
<FromGitter> <plambert> If I copy each element to an Array(String | Int32 | Nil) and attempt to append that… it still fails?
<FromGitter> <plambert> Oh, duh. dumb typo. nevermind. :)
<FromGitter> <plambert> Nope, no typo. It *should* work, shouldn't it?
<FromGitter> <plambert> Crystal 1.6.0. Let me try 1.6.1.
<FromGitter> <plambert> Oh, I have a meeting, sorry, I'll be non-responsive for 40 minutes…
<FromGitter> <Blacksmoke16> i would have expected `@table.concat` to work, which if you `typeof` it, its `Array(Int32 | String | Nil)` ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ 🤔 []
<FromGitter> <Blacksmoke16> (prob want to use concat btw, avoids intermediary arrays)
<FromGitter> <Blacksmoke16> oh duh, just have to do `@table <<`
<FromGitter> <Blacksmoke16> otherwise its trying to concat each value in the array, to concat the array itself would have to nest it in another array
<FromGitter> <plambert> Back! Thank you, I'll try that.
<FromGitter> <plambert> I still feel like, for a *literal array*, this shouldn't happen. The compiler should be able to make its type match its only use case, since it's a literal. I totally get that passing a non-literal would not be permitted, since the compiler doesn't know what types might later be inserted. Or if the literal explicitly said it didn't allow Nil…
<FromGitter> <Blacksmoke16> I'd read thru that link i posted earlier. Goes through this scenario. Tho my bad, i initialy forgot parameter restrictions are a bit less strict
<FromGitter> <plambert> Thanks, I will!
<FromGitter> <plambert> And thanks for all your help.
<FromGitter> <plambert> I really love crystal. I've been programming since Perl 4, and it's definitely my favorite language.
<FromGitter> <Blacksmoke16> 💪
<FromGitter> <Blacksmoke16> Np