<postmodern>
if you assign a new Struct or Value type object to a constant, and pass that constant to a method, is it passed by value or reference?
<FromGitter>
<oprypin:matrix.org> postmodern, by value
<postmodern>
i noticed that Set(T) inherits from Struct. I want to create a class that acts like a Set(T), with additional methods, and pre-define various types of these sets to constants, but not have them be passed by value because that would end up copying a lot of data onto the stack.
<FromGitter>
<naqvis> postmodern you can wrap `Set(T)` inside your custom class
DTZUZU_ has joined #crystal-lang
<FromGitter>
<naqvis> composition is much more better than inheritance :P
DTZUZU has quit [Ping timeout: 240 seconds]
<postmodern>
naqvis, i could, then maybe delegate all the set operation methods?
<FromGitter>
<naqvis> yeah
<FromGitter>
<naqvis> or `forward_missing_to`
<FromGitter>
<naqvis> though if you want to expose a specific methods, then `delegate` would be best option
DTZUZU has joined #crystal-lang
DTZUZU_ has quit [Ping timeout: 240 seconds]
andremedeiros has quit [Read error: Connection reset by peer]
andremedeiros has joined #crystal-lang
<postmodern>
can you define the type of a *splat argument?
<FromGitter>
<naqvis> not sure I understand
<postmodern>
something like `def self.[](*values : Array(Char | Byte))`?
<FromGitter>
<Blacksmoke16> would ensure each arg is a char or byte
<postmodern>
nice
<postmodern>
also is there a method to get the UInt word form of a given Char? According to the docs Char#to_i only works if the char is ASCII. I want to support getting the integer word form of UIT16/UTF32 chars
<postmodern>
er multi-byte chars. I remember crystal only supports UTF8, but I assume it's possible to have a multi-byte char?
<postmodern>
er wait, Char#ord looks like what i want
<postmodern>
odd that Char#ord returns Int32. I see lots of things in stdlib use Int32, when UInt32 or UInt8 might be more suitable.
<FromGitter>
<Blacksmoke16> Int32 is the default because there isnt much diff than unsigned ints and prevents your from shooting yourself in the foot
<postmodern>
except that most-significant signedness bit
<postmodern>
*bits if it's sign-extended
<postmodern>
why doesn't Set's Enumerable methods return an Array and not a new Set?
<postmodern>
is this because we want to allow Set#map to return an Array containing duplicates?
<postmodern>
Set#select could technically return another Set
DTZUZU has quit [Read error: Connection reset by peer]
<postmodern>
also noticed some things in stdlib use Int while others use Int32?
andremedeiros has quit [Read error: Connection reset by peer]
andremedeiros has joined #crystal-lang
f1reflyylmao has joined #crystal-lang
f1refly has quit [Ping timeout: 240 seconds]
f1reflyylmao is now known as f1refly
<FromGitter>
<HertzDevil> `dup.select!`
<FromGitter>
<HertzDevil> looks like lib funs don't support autocasting of symbols to lib enums
<postmodern>
why can't you define methods where an argument is pinned to a literal value, and have the overloading matcher select that method based on the literals. `def do_stuff(data : String, offset : true) vs. def do_stuff(data : String)`
<FromGitter>
<naqvis> can you elaborate more with examples from existing languages which support such use-case?
<FromGitter>
<naqvis> i would say Crystal is already flexible enough where it doesn't require one to provide types during method declaration, while all other languages which comes with inference capability, mandates types on method definition, as that's the only thing where compiler doesn't prefer to infer types at call-site
<postmodern>
naqvis, i want to have an altnernate method that returns a Hash instead of an Array if `offsets: true`. Also being able to match argument literals allows us to do really cool stuff, like what Prolog and Haskell can do.
<FromGitter>
<naqvis> what's prohibiting you from using default arguments?
<postmodern>
if `offset: false`, then the method shouldn't return a Hash
<FromGitter>
<naqvis> but personally I will prefer not to go such route, as that's confusing to user of such library
<postmodern>
would also be nice if the method overload matcher could match the return type, that way i could do `result : Hash = do_stuff(data)` and it would use the Hash version of the method.
<postmodern>
you can't define a variable who's type is returned by another method call, can you? ex: `word : word_type(bytesize) = 0`
<postmodern>
is there a constant for \n vs \r\n new-line character?
DTZUZU has joined #crystal-lang
<FromGitter>
<naqvis> overloading works on argument types, not on return type
<FromGitter>
<naqvis> you don't need to provide a type for variable, as it will be inferred by the compiler
<FromGitter>
<erdnaxeli:cervoi.se> `instance_vars` returns the type's attribute, and it must be executed in a method to works, else it returns nothing
<FromGitter>
<erdnaxeli:cervoi.se> i don't know any way to get local variables
<FromGitter>
<erdnaxeli:cervoi.se> I think that when macros are executed the compiler does not know local variables already (and a macro could generate new local variables)
<FromGitter>
<erdnaxeli:cervoi.se> hmm, yeah, I don't know how to do it without parsing the user input to detect variables declaration. Maybe the compiler expose methods to parse crystal code and get the AST?
<FromGitter>
<Blacksmoke16> the easiest solution here is to not use icr :P problem solved
<FromGitter>
<Blacksmoke16> just have a `test.cr` file and run it
<FromGitter>
<Daniel-Worrall> icr be janky
<FromGitter>
<dineshgadge> I want to load some values from a config.yml file. When I am shipping a binary, how do I load the config.yml file relative to the binary? (as opposed to the src folder)
<FromGitter>
<Blacksmoke16> it would be possible to include the source of the config file in the binary
<FromGitter>
<Blacksmoke16> then thats all you need to ship, but i hope you arent actually shipping the source code with the binary yea?
<FromGitter>
<dineshgadge> no I am not shipping the source code ..
<FromGitter>
<Blacksmoke16> good :)
<FromGitter>
<dineshgadge> but I want the client to be able to change some config settings using a config file ..
<FromGitter>
<dineshgadge> thanks @wyhaines .. I'll try this out ..
<FromGitter>
<wyhaines> Dir.current returns the current working directory. So, something like `File.join(Dir.current, "config.yml")` will give you what you need.
<straight-shoota>
@Blacksmoke16 Embedding a YAML file into the binary wouldn't be useful at all. You could just embed data directly without YAML
<FromGitter>
<dineshgadge> @Blacksmoke16 let me check if pseudo constants work better for me .. or Dir.current as @wyhaines suggested
<FromGitter>
<Blacksmoke16> 👍
<FromGitter>
<Blacksmoke16> even if you have another build process generate the yaml file?
<straight-shoota>
current directory won't help for a path relative to the executable location
<straight-shoota>
@Blacksmoke16 Yeah, you can still parse it at compile time
<FromGitter>
<Blacksmoke16> wait, yea thats what i meant. I.e. with `read_file`
<straight-shoota>
pseudo constants are compile time values, so they don't help at all to figure out runtime paths
<FromGitter>
<Blacksmoke16> couldnt you just put the config file next to the binary and do like `"./config.yml"`?
<straight-shoota>
that only works when you call the executable as ./program
<FromGitter>
<Blacksmoke16> wouldnt that be like `Hash(String, Proc(Int32), Hash(Int32, Int32))`?
<FromGitter>
<Blacksmoke16> sorry `|` as the value
<FromGitter>
<Blacksmoke16> could maybe have another type that wraps it and defines `#[]` that checks if the value is a proc and calls it or just returns the value?
<postmodern>
correct, the error i get is `Error: undefined method '[]' for Proc(UInt8, UInt8) (compile-time type is (Hash(Int32, Int32) | Proc(UInt8, UInt8)))`
<FromGitter>
<Blacksmoke16> can you make a little playground example?
<postmodern>
in a bit
<FromGitter>
<asterite> I think adding `#[]` to Proc might be nice. If a method argument is a Hash, or quacks like a hash, making it also work with Proc is nice
<FromGitter>
<asterite> It should also be really simple. Maybe you can add it yourself for now. I think `def [](*args); call(*args); end` might work
<postmodern>
i guess the only difference would then be that Hash can sometimes return Nil, while Proc is usually guaranteed to return non-Nil (unless it explicitly contains an if/else branch)
<postmodern>
also I noticed that IO::ByteFormat uses IO#read_fully. This prevents it from doing partial reads (i.e you're trying to convert some bytes to a uint32, but only have 3 bytes left)
<postmodern>
i wonder if copying the IO::ByteFormat code which casts a buffer and reverses it based on endianness would be faster that just shifting in each byte into a word as I read them
<FromGitter>
<naqvis> i guess you have a misconception on `IO::ByteFormat#decode` doing `IO#read_fully` :P
<FromGitter>
<naqvis> `read_fully` will read to the size of passed buffer
<FromGitter>
<naqvis> so buffer#size is that of bytesize of type you have passed
<postmodern>
naqvis, i meant the length of the input buffer, which may not be evenly divisible by the sizeof(type): https://carc.in/#/r/auqx
hightower2 has joined #crystal-lang
<FromGitter>
<naqvis> that means encoding is not standard and you shouldn’t be using standard approach. instead do a custom decoding or follow the encoding approach to perform decoding
<FromGitter>
<Blacksmoke16> is the forum broken for anyone else? When i open a thread all the replies disappear 🤔. Console suggests its some issue with the service worker
<straight-shoota>
works for me
<FromGitter>
<Blacksmoke16> hm, let me clear cache and stuff
<straight-shoota>
might be a user client issue. try remove the service worker locally
<FromGitter>
<Blacksmoke16> ok interesting, only happens when im logged in
<FromGitter>
<Blacksmoke16> and when using the dark theme