ChanServ changed the topic of #crystal-lang to: The Crystal programming language | https://crystal-lang.org | Fund Crystal's development: https://crystal-lang.org/sponsors | GH: https://github.com/crystal-lang/crystal | Docs: https://crystal-lang.org/docs | Gitter: https://gitter.im/crystal-lang/crystal
ur5us__ has joined #crystal-lang
ur5us_ has quit [Ping timeout: 252 seconds]
ur5us__ has quit [Ping timeout: 256 seconds]
ur5us__ has joined #crystal-lang
ur5us__ has quit [Ping timeout: 260 seconds]
ur5us__ has joined #crystal-lang
ur5us__ has quit [Ping timeout: 246 seconds]
ur5us__ has joined #crystal-lang
ur5us__ has quit [Ping timeout: 246 seconds]
Sankalp has quit [Ping timeout: 268 seconds]
Sankalp has joined #crystal-lang
Sankalp has quit [Ping timeout: 252 seconds]
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #crystal-lang
Sankalp has joined #crystal-lang
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #crystal-lang
jmdaemon has quit [Ping timeout: 252 seconds]
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #crystal-lang
notzmv has quit [Ping timeout: 256 seconds]
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #crystal-lang
notzmv has joined #crystal-lang
alexherbo2 has quit [Remote host closed the connection]
_ht has joined #crystal-lang
<FromGitter> <azurelmao> How can I fix this? ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=63a1f0b4fb195421bd482e7a]
<FromGitter> <azurelmao> adding T type restrictions in the method params didn't help
<FromGitter> <azurelmao> sometimes it's such a pain having to cast everything to the desired type
<FromGitter> <azurelmao> why can crystal do it automatically for literals but not for variables
<FromGitter> <Blacksmoke16> prob because you're passing it `3.14` and not `3.14_f32`
<FromGitter> <Blacksmoke16> ah right
<FromGitter> <RespiteSage> I think that makes sense. You can't necessarily "fit" a Float64 into a Float32, so you'd need explicit conversion. If you want it to look like implicit conversion when creating the vector, you can put the conversion inside the constructors, but I'd just use Float32 wherever you're constructing the vector so your values don't change when they get turned into a vector.
<FromGitter> <Blacksmoke16> ^ i agree
<FromGitter> <Blacksmoke16> works for literals prob since the compiler can ensure it'll fit, versus a var which could be any size
<FromGitter> <azurelmao> fair
<FromGitter> <azurelmao> okay I can do .as(T) at least
<FromGitter> <Blacksmoke16> thats prob not what you want. `.as` is generally for reducing the size of a union type, not converting a value from one type to anoterh
<FromGitter> <Blacksmoke16> another*
<FromGitter> <azurelmao> there is no .to_(T) is there?
<FromGitter> <Blacksmoke16> id do something like
<FromGitter> <Blacksmoke16> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=63a1f7637de82d2616ee7b2a]
taupiqueur has quit [Quit: WeeChat 3.7.1]
<FromGitter> <azurelmao> But I want both Float64 and Float32 types
<FromGitter> <azurelmao> I'm only using Float32 cause of OpenGL
<FromGitter> <azurelmao> since that's what it uses
<FromGitter> <Blacksmoke16> right, thats what the `self.new` overload on `Float` is for
<FromGitter> <Blacksmoke16> if you give it a `Float64` it'll call `.to_f32` on it
<FromGitter> <azurelmao> but that will decrease my precision
<FromGitter> <azurelmao> what's the point then
<FromGitter> <azurelmao> could i do something like this? ⏎ ⏎ ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=63a1f9017de82d2616ee7ee0]
<FromGitter> <azurelmao> though that causes infinite recursion, i can swap the name for []
<FromGitter> <Blacksmoke16> but i thought you were storing them internally as `Float32`?
<FromGitter> <azurelmao> > How can I fix this? ⏎ > ```cr ⏎ > struct Vec3(T) ⏎ > property x : T ⏎ > property y : T ... [https://gitter.im/crystal-lang/crystal?at=63a1fa8ac4fec572d648954a]
<FromGitter> <Blacksmoke16> right, so if you `Vec3(Float32)` and you give it a `Float64` what is your expectation?
<FromGitter> <Blacksmoke16> that it casts them to an `f32` or?
<FromGitter> <azurelmao> ah, my logic is flawed
<FromGitter> <azurelmao> wait no
<FromGitter> <azurelmao> yeah if T is Float32 and I give it any number it should convert it to a float32
<FromGitter> <Blacksmoke16> and i assume there is a case where `T` will be something other than `Float32`?
<FromGitter> <azurelmao> yeah, like I said I want both
<FromGitter> <Blacksmoke16> in this case i wouldnt worry about it and just handle it where you're instantiating it
<FromGitter> <azurelmao> I use Vec3(Float64) for server stuff and Vec3(Float32) for opengl
<FromGitter> <Blacksmoke16> `Vec3(Float32).new x.to_f32, y.to_f32, z.to_f32`
<FromGitter> <azurelmao> I could do that, but the whole point of the [] method was to provide a clean way to write it
<FromGitter> <Blacksmoke16> new idea
<FromGitter> <Blacksmoke16> sec
<FromGitter> <azurelmao> so I can do Vec3f[0, 0, 1] and it will do Vec3(Float32).new(0.0_f32, 0.0_f32, 1.0_f32)
<FromGitter> <azurelmao> you see how better that looks?
<FromGitter> <Blacksmoke16> ```def self.[](x, y, z) ⏎ Vec3(T).new(T.new(x), T.new(y), T.new(z)) ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=63a1fbd00dba35742339e9be]
<FromGitter> <Blacksmoke16> `T.new` calls the related `to_*` method on the passed in value
<FromGitter> <azurelmao> ooo, I'll try that
<FromGitter> <Blacksmoke16> e.g. `Float32.new` => `to_f32`, `Int64.new` => `to_i64`
<FromGitter> <Blacksmoke16> might want to add an assertion or something like `{% T.raise "Vec3 may only be used with numeric types" unless T <= Number %}` to prevent like `Vec3(String)`
<FromGitter> <azurelmao> sure
<FromGitter> <azurelmao> yay, it works!
<FromGitter> <azurelmao> Thank you!!
<FromGitter> <Blacksmoke16> 👍
<FromGitter> <azurelmao> 🐐
notzmv has quit [Ping timeout: 265 seconds]
ur5us__ has joined #crystal-lang
_ht has quit [Quit: _ht]
jmdaemon has joined #crystal-lang
sagax has quit [Ping timeout: 260 seconds]
notzmv has joined #crystal-lang
jmdaemon has quit [Quit: ZNC 1.8.2 - https://znc.in]
jmdaemon has joined #crystal-lang