andremedeiros has quit [Read error: Connection reset by peer]
andremedeiros has joined #crystal-lang
ua has joined #crystal-lang
<FromGitter>
<lodenos> Hey Guy’s I’ve a wierd question to ask why `Slice#.new(pointer : Pointer(T), size : Int, *, read_only = false)` size is a Int not a UInt ?
<straight-shoota>
The method allows to pass any int type, but it gets automatically casted to the internal type
<straight-shoota>
internal size is still Int32 btw.
<FromGitter>
<lodenos> Yes but UInt it’s more logical
<FromGitter>
<lodenos> That mean the maximum size of Slice(Int8) = 2_147_483_648 Bytes
<straight-shoota>
stdlib APIs try to avoid unsigned integers to prevent you from shooting yourself in the foot.
<straight-shoota>
It's just to easy to use slice.size in algebra operations and you get a real mess when subtraction raises an overflow, just because you go below 0
<straight-shoota>
and UInt32 wouldn't be a real solution either, because you still couldn't have slices bigger than 4_294_967_295 bytes while 64-bit architectures allow much more
<straight-shoota>
So the idea is to increase the size to Int64 instead (the difference between Int64 and UInt64 is negligible)
<straight-shoota>
there's already a PR for that, but the exact migration procedure is not yet finalized
<FromGitter>
<lodenos> I see I understant why this choise, My question seem weird but just for understand the choice behind
<straight-shoota>
the question is perfectly reasonable
<straight-shoota>
:D
* FromGitter
* tenebrousedge lurks
<FromGitter>
<resuni:matrix.org> How do you deal with a situation where a function could return a number of given types? Simple example: https://play.crystal-lang.org/#/r/at75
<FromGitter>
<Blacksmoke16> do type checking to ensure its what you want/expect
<FromGitter>
<Blacksmoke16> or just dont do that
<FromGitter>
<tenebrousedge> `case` works to sort out types
<FromGitter>
<tenebrousedge> but you might want to have some mixin class there
<FromGitter>
<Blacksmoke16> whats the end goal here? the error there is because `@object` can be nil, when its defined that it cant
<FromGitter>
<Blacksmoke16> then if you fix that `CallingClass` doesnt have `.var1` method so that wouldnt work either
<FromGitter>
<Blacksmoke16> are a few ways to solve this, but it would help to know the end goal first
<FromGitter>
<tenebrousedge> avoiding initializing properties as nil is pretty good too
<FromGitter>
<tenebrousedge> letting anything be `nil` that doesn't have to be is just obnoxious
<FromGitter>
<resuni:matrix.org> I'm in a situation where I have to instantiate from a different class depending on what I'm parsing.
<FromGitter>
<Blacksmoke16> maybe setup some sort of interface base class, then have implementations for each "type" of thing?
<FromGitter>
<Blacksmoke16> versus having 1 type with a big union of everything, you could just type it as the base type
<FromGitter>
<resuni:matrix.org> When I use if statements or case statements, the compiler still sees the possibility that the conditions aren't met and complains about nil.
<FromGitter>
<tenebrousedge> yee
<FromGitter>
<Blacksmoke16> got an example of that @resuni:matrix.org?
<FromGitter>
<Blacksmoke16> prob just arent using them correctly or something
<FromGitter>
<resuni:matrix.org> Well, I haven't tried using a case statement that actually looks at the type. Should I use #is_a? to do that? https://crystal-lang.org/api/1.0.0/Object.html#is_a?(type:Class):Bool-instance-method
<FromGitter>
<tenebrousedge> nah just use `case Foo`
<FromGitter>
<resuni:matrix.org> I don't think class inheriting like that is an option in my case, but I'll think on it.
<FromGitter>
<Blacksmoke16> 👍
<FromGitter>
<tenebrousedge> could you use a module?
<FromGitter>
<tenebrousedge> Goddess I've missed thinking about code
<FromGitter>
<resuni:matrix.org> I have to go in a few minutes, but one question about the case statement: Why does that work with a local variable but not the getter inside the object?
<FromGitter>
<resuni:matrix.org> > procs and methods aren't guaranteed to return the same more-specific type on two successive calls
<FromGitter>
<resuni:matrix.org> Why does assigning a local variable change this?
<FromGitter>
<Blacksmoke16> because it makes things based on the return value of the first call
<FromGitter>
<Blacksmoke16> i.e. its not going to change
<FromGitter>
<Blacksmoke16> as its not another method call you know?
<FromGitter>
<tenebrousedge> that does make sense
<FromGitter>
<resuni:matrix.org> ok, I think I understand.
<FromGitter>
<resuni:matrix.org> I'll probably be back later with more questions about that lol
<FromGitter>
<Blacksmoke16> 👍
hendursaga has quit [Ping timeout: 240 seconds]
DTZUZU has joined #crystal-lang
<FromGitter>
<resuni:matrix.org> I made a mistake in my first example. The case statement is actually in a second function in CallingClass: https://play.crystal-lang.org/#/r/at87
<FromGitter>
<resuni:matrix.org> In which case reassigning to a local variable doesn't make sense.
<FromGitter>
<resuni:matrix.org> > The value of these kinds of variables could potentially be affected by another fiber after the condition was checked
<FromGitter>
<resuni:matrix.org> What is a fiber?
<FromGitter>
<resuni:matrix.org> Is it like a thread?
<FromGitter>
<resuni:matrix.org> I think that clears up my confusion about why assigning to a local variable works then. I wasn't thinking about concurrency.
<FromGitter>
<tenebrousedge> I mean you could also just have a method that returns "1" or "2" on alternate calls
<FromGitter>
<tenebrousedge> the result of two subsequent calls doesn't have to be remotely similar
hendursaga has joined #crystal-lang
Welog has quit [Remote host closed the connection]