laaron has quit [Remote host closed the connection]
<alex```>
how to print '\0' ?
<alex```>
s/print/emit/
<alex```>
oh just "\0"
laaron has joined #crystal-lang
<alex```>
'\0' doesn't work?
<FromGitter>
<Blacksmoke16> its trying to escape the 0
<FromGitter>
<Blacksmoke16> the 0 makes it think its a unicde sequence
<FromGitter>
<Blacksmoke16> `A backslash followed by at most three digits ranging from 0 to 7 denotes a code point written in octal:`
lucasb has joined #crystal-lang
<FromGitter>
<bew> @watzon I think I know why your uuid thing do that
<FromGitter>
<Blacksmoke16> oh?
<FromGitter>
<bew> The problem is that `UUID` is a struct, which means that when you pass it to a method, it is copied.. Now when you do `to_slice` on a uuid, it returns a slice of the internal StaticArray of UUID, which is local to the function (on the stack).
<FromGitter>
<bew> so by accessing it after the method call you're basically accessing a part of the stack that's not 'yours'
<FromGitter>
<Blacksmoke16> interesting
<FromGitter>
<Blacksmoke16> but shouldnt the bytes be the same even if it was copied? the bytes that make up the uuid should be the same no?
<FromGitter>
<bew> inside the function they are the same, that's why when you print the to_slice inside the function it's still ok
<FromGitter>
<bew> but when you're outside, the area used by local variables for the function is "freed" and is used for other things
<FromGitter>
<Blacksmoke16> ah so the returned slice do longer represents the uuid
<FromGitter>
<Blacksmoke16> `value.to_slice.dup` and this works id imagine because dup allocate a section of memory specifically for the bytes?
<FromGitter>
<bew> I don't have time now, but if someone can report this back in the form of an issue, I think we should fix that..
<FromGitter>
<bew> yes @Blacksmoke16
<FromGitter>
<Blacksmoke16> 👍 yea i can make an issue later today
<FromGitter>
<Blacksmoke16> def not what someone would expect to happen from that
<FromGitter>
<naqvis> Hi All, what is idiomatic way to write equality function on types with optional fields (nilable)? I understand `if var` has limitations on instance and/or class variables, and one can circumvent that via local assignment, but that does clutter the code for complex fields. for exmaple, below one liner won’t pass compiler check, as `field` is optional, and it might have Nil. ⏎ ⏎ ```if other.field &&
<FromGitter>
... (other.field.to_set subset? field.to_set)``` ⏎ ⏎ May be i’m missing some steps or I have to go with local assignment for both objects prior to invoking truthy condition? Any comments and suggestions are highly appreciated. [https://gitter.im/crystal-lang/crystal?at=5d07a76f4e8f972d37369c26]
<FromGitter>
<Blacksmoke16> or could do something like `if (f = other.field) && (f.to_set subset? f.to_set)`
Yxhuvud has quit [Quit: No Ping reply in 180 seconds.]
<FromGitter>
<naqvis> Thanks @Blacksmoke16 , problem arises when performing action on local object. In your example last `f.to_set` should be local optional object and that is where compiler throws error
<FromGitter>
<naqvis> I just tried setting default empty array and compiler is still yielding the same error. Is empty array equavalent to Nil?
<FromGitter>
<Blacksmoke16> get rid of nilable
<FromGitter>
<Blacksmoke16> point being its not going to be nilable if you give it a default
<FromGitter>
<naqvis> that’s true and outputting the variable does shows that empty array is assigned. So by having default, i should remove nilable?
<FromGitter>
<Blacksmoke16> yes because its not nilable anymore
<FromGitter>
<Blacksmoke16> unless you want it to be, which you do not
<FromGitter>
<naqvis> thanks, seems I was confused with the documentation of `default` key. I thought having default value will override that, but seems this is not the case
<FromGitter>
<naqvis> *default*: value to use if the property is missing in the JSON document, or if it's `null` and `nilable` was not set to `true`. If the default value creates a new instance of an object (for example `[1, 2, 3]` or `SomeObject.new`), a different instance will be used each time a JSON document is parsed.
<FromGitter>
<Blacksmoke16> well no, it can have a default but be nilable
<FromGitter>
<Blacksmoke16> like if you kept nilable: true you could still do like
<FromGitter>
<naqvis> doesn’t that contradict?
<FromGitter>
<naqvis> when we have default value how that can be nil?
<FromGitter>
<Blacksmoke16> obj.features = nil
<FromGitter>
<Blacksmoke16> since the *type* of features still allows nil
<FromGitter>
<naqvis> so Union is in effect here
<FromGitter>
<naqvis> make sense. Thank you @Blacksmoke16
<FromGitter>
<Blacksmoke16> thats what nilable does, just adds `Nil` to the property's type
<FromGitter>
<naqvis> Thank you
<FromGitter>
<Blacksmoke16> np
<FromGitter>
<naqvis> one more question though, what is equalvalent of `this` in Crystal? i.e. how can we refer to same object?
<FromGitter>
<naqvis> my intention is to pass current obj to target object
<FromGitter>
<Blacksmoke16> whats the error you're getting?
<FromGitter>
<naqvis> I’m expecting a boolean, but this is giving me something different
<FromGitter>
<Blacksmoke16> dont need the `return` keyword as well
<FromGitter>
<naqvis> yeah sure
<FromGitter>
<Blacksmoke16> got an example of what you're getting?
<FromGitter>
<naqvis> removing that `return` is returning bool, while having `return` was returning `(NamedTuple | Nil)`
<FromGitter>
<Blacksmoke16> :thinking:
<FromGitter>
<Blacksmoke16> can you make another playground link?
<FromGitter>
<naqvis> yeah, doing that
<FromGitter>
<naqvis> sorry, can’t reproduce that, seems I might have messed up on my local playground :(
<FromGitter>
<Blacksmoke16> rgr
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
<FromGitter>
<PlayLights_twitter> Hello :) Is `Mutex` ready for use? I see on the class definition `TODO this is not thread-safe yet.`but I need it
<FromGitter>
<Blacksmoke16> its mainly for fibers atm since crystal doesnt have mutli threads yet anyway
rohitpaulk has joined #crystal-lang
<FromGitter>
<PlayLights_twitter> Since I'm using Kemal and it use fibers, it supposed it should work right?
<FromGitter>
<Blacksmoke16> yes but what are you trying to do, a request in one fiber shouldnt affect another request
<FromGitter>
<naqvis> Can we not explicitly type annotate constants? as annotating constant is yielding `unexpected token: :`
<FromGitter>
<Blacksmoke16> hm?
<FromGitter>
<Blacksmoke16> example?
<FromGitter>
<Blacksmoke16> like `FOO : String = "Fred"`?
<FromGitter>
<naqvis> ```TEST : String = “abc”```
<FromGitter>
<Blacksmoke16> there isnt really a point
<FromGitter>
<Blacksmoke16> it get inferred from the value, and you cant change it
<FromGitter>
<Blacksmoke16> so its kinda redundent
<FromGitter>
<naqvis> yeah I know, but what if i want to annotate my own type?
<FromGitter>
<naqvis> could be an alias or could be something
<FromGitter>
<PlayLights_twitter> @Blacksmoke16 I mean there is an issue on crystal-db where too many fibers are creating connections at the same time overpassing the`max_connections`. So I tried using a lock with a variable, but on high load on a Xeon it still doesn't work so I think using mutex should work to avoid that.
<FromGitter>
<PlayLights_twitter> (I'm planning to buy the 'The art of concurrency book', all of this is pretty interesting)
<FromGitter>
<Blacksmoke16> hmm
<FromGitter>
<Blacksmoke16> @naqvis not sure what you mean, if you assign something to a constant the type of the constant would just be what you assign it
<FromGitter>
<Blacksmoke16> do you have an example of what you are trying to do?
<FromGitter>
<naqvis> Yeah, I understand that. But what if like to define different types, for example for my use case I could type alias String to `MediaType`
<FromGitter>
<Blacksmoke16> @PlayLights_twitter yea i dunno, not super familiar with that, are you using the shard directly or via an ORM?
<FromGitter>
<naqvis> kind of creating own types and using that terminology through the code, to make that easier to read
<FromGitter>
<naqvis> thank you so much :). So have to cast that via `as`
<FromGitter>
<Blacksmoke16> yes
<FromGitter>
<naqvis> big thanks
<FromGitter>
<Blacksmoke16> np
lucasb has quit [Quit: Connection closed for inactivity]
alex``` has quit [Quit: WeeChat 2.5]
<FromGitter>
<PlayLights_twitter> @Blacksmoke16 im using crystal-db, cystal-mysql, and granite, but the issue is from crystal-db #77 (https://github.com/crystal-lang/crystal-db/issues/77), I'll try to use Mutex to see if it works.
<FromGitter>
<Blacksmoke16> gotcha, good luck
<FromGitter>
<naqvis> interesting enough, printing type is still going to print its base type, this is totally different from other languages, where compiler will treat UDT differently. pseudo_method `is_a? returns true for both alias as well as base type. While in different languages same case doesn’t stand.
<FromGitter>
<naqvis> so is this alias just a compile-time syntatic sugar?
<FromGitter>
<Blacksmoke16> i dunno, imo it makes sense that it should pass
<FromGitter>
<Blacksmoke16> since the type is just a diff name to a string type
rohitpaulk has quit [Remote host closed the connection]
<FromGitter>
<naqvis> agree as we are unable to extend types, so technically speaking alias is just same as base type, its just a syntactical sugar for developers to use. While go does allow extending types, so at runtime two types might not be same
<FromGitter>
<Blacksmoke16> main benefit of alias is to shorten types and can also do recursive types (although that isnt usually the best option)
<FromGitter>
<naqvis> apologies for irrelevant questions, as I might be in process of adjusting my brain to this amazing language
<FromGitter>
<Blacksmoke16> np
<FromGitter>
<Blacksmoke16> `Every time you use an alias the compiler replaces it with the type it refers to.`
<FromGitter>
<Blacksmoke16> so since your alias is just `String` its essentially the same as doing `.as(String)`
<FromGitter>
<naqvis> that clarifies, so aliases are lived till compile time
<FromGitter>
<Blacksmoke16> yea
<FromGitter>
<naqvis> based on today discussion, does that mean that we won’t have RTTI? or that is something planned for future?
devil_tux has joined #crystal-lang
<FromGitter>
<Blacksmoke16> there is no runtime reflection atm, but can use `typeof` to get the type of a variable
<FromGitter>
<watzon> Just cleans things up a tiny bit imo
<FromGitter>
<naqvis> 👏
devil_tux has quit [Ping timeout: 248 seconds]
devil_tux has joined #crystal-lang
devil_tux has quit [Ping timeout: 248 seconds]
devil_tux has joined #crystal-lang
devil_tux has quit [Ping timeout: 244 seconds]
<FromGitter>
<naqvis> when defining a new type, what method need to be overridden to allow casting to type? currently i’m getting `can't cast Foo to Bar`
<FromGitter>
<naqvis> this is received when pseudo method `as` is invoked
<FromGitter>
<naqvis> i can’t override pseudo method, so believe there should be some other way to achieve the same
<FromGitter>
<Blacksmoke16> well if they arent compatible types i dont think you can
<FromGitter>
<naqvis> same way, when using `from_json` i’m receiving that error
<FromGitter>
<naqvis> `can’t cast String to FakeString`
<FromGitter>
<Blacksmoke16> well you cant directly cast String to FakeString, they are compatible in the same way you can do `true.as(String)`
<FromGitter>
<naqvis> thanks
<FromGitter>
<Blacksmoke16> what are you trying to do?
<FromGitter>
<naqvis> in JSON.mapping i’ve provided that derived type, implemented json marshalling method one which accepts `JSON::PullParser`, now when invoking instance via `from_json`, macro expanding is failing with above cited error
<FromGitter>
<naqvis> seems I might have to revert back to String
<FromGitter>
<watzon> Why are you trying to use `FakeString` anyway?
<FromGitter>
<Blacksmoke16> ^
<FromGitter>
<watzon> What's the poine?
<FromGitter>
<naqvis> my use case is String containing two parts like “algo:digest”, so I thought of doing that in respective derived type, instead of treating that as string
<FromGitter>
<naqvis> I’m working on json parser to work with end-point
<FromGitter>
<Blacksmoke16> could use an enum
<FromGitter>
<Blacksmoke16> er nvm
<FromGitter>
<naqvis> but enum is restricted to integer values
<FromGitter>
<naqvis> at least that’s what I understand from documentation
<FromGitter>
<Blacksmoke16> yea
<FromGitter>
<Blacksmoke16> you got a playground link of what you got so far?
<FromGitter>
<Blacksmoke16> i have an idea
<FromGitter>
<naqvis> let me put that in playground
<FromGitter>
<Blacksmoke16> was that something you can use?
<FromGitter>
<naqvis> yeah, definitely
<FromGitter>
<Blacksmoke16> cool
<FromGitter>
<naqvis> i’m looking into that
<FromGitter>
<naqvis> thanks again @Blacksmoke16
<FromGitter>
<Blacksmoke16> np
duane has quit [Ping timeout: 245 seconds]
devil_tux has joined #crystal-lang
<FromGitter>
<naqvis> @Blacksmoke16 actually I was missing `converter` key in my `JSON.mapping`. I’ve implemented converter interface and am able to use my own types
<FromGitter>
<naqvis> 😃
<FromGitter>
<Blacksmoke16> fair enough
<FromGitter>
<naqvis> thank you again for all patience and guidance in right direction
<FromGitter>
<watzon> Which I know we can't do in Crystal yet
lvmbdv has quit [Quit: brb]
laaron has quit [Remote host closed the connection]
laaron has joined #crystal-lang
ashirase has quit [Ping timeout: 246 seconds]
<FromGitter>
<omidathari> Hi All, ⏎ In the code below, would you expect the pointer to the first byte of the string to be the same as the pointer of the string ⏎ https://carc.in/#/r/734m
<FromGitter>
<watzon> I don't think so. I'm fairly certain there is some additional metadata allocated before the first byte of the string
<FromGitter>
<omidathari> @watzon Isn't it interesting that the first byte is actually one byte behind the pointer of the string though?
<FromGitter>
<watzon> You should see what the value of the real first byte is
<FromGitter>
<watzon> I'd be willing to bet it's always the same value. I think it's metadata saying "what comes after is a string" or something