ChanServ changed the topic of #crystal-lang to: The Crystal programming language | | Fund Crystal's development: | GH: | Docs: | Gitter:
postmodern has joined #crystal-lang
hightower3 has quit [Read error: Connection reset by peer]
<Na_Klar> okay, this is pretty basic, but I have to get my head into how to script for compilers: If I have a variable which can be Nil or String, I don't know how I can access its value if it is not nil and process it further. if (str != nil) str.strip end will fail with no such method at compile time ofc. since at compile time it is Nil, that I understand. but how do I acces its value if it is not nil?
<FromGitter> <Blacksmoke16> there are quite a few ways you can go about it, all depends on context which is the best solution
<Na_Klar> what's the most basic way? just that I understand the logic.
<FromGitter> <Blacksmoke16> ```str.try do |s| ⏎ # s is not nil ⏎ end``` []
<Na_Klar> ah try ... I see
<FromGitter> <Blacksmoke16> ```if s = str ⏎ # s is not nil ⏎ end``` []
<FromGitter> <Blacksmoke16> a question to ask yourself, does it actually *need* to be nilable? removing `Nil` from the union would solve everything
<FromGitter> <Blacksmoke16> ofc are valid reasons for it to be nilable, but is worth thinking about at least
<Na_Klar> a stdin read must be nil if there is no user input e.g.
<Na_Klar> but how is "unless str.nil?" different from "if (str != nil)"?
<FromGitter> <Blacksmoke16> `#nil?` is a special method provided by the compiler which is able to do more than simple comparison
<Na_Klar> I see, I see ..
<FromGitter> <Blacksmoke16> it's essentially doing like:
<FromGitter> <Blacksmoke16> ```unless str.is_a? Nil ⏎ # str is not nil ⏎ end``` []
<FromGitter> <Blacksmoke16>
<Na_Klar> so "if (str.nil? != true)" would be the same?
<FromGitter> <Blacksmoke16> `if !str.nil?` is the same as `unless str.nil?`
<FromGitter> <Blacksmoke16> fwiw
<FromGitter> <Blacksmoke16> and in Crystal `nil` is an object like everything else. I.e. an instance of `Nil` type
<FromGitter> <Blacksmoke16> which is why `is_a? Nil` works. Can think of it like `$str instanceof Nil` in other langs
<FromGitter> <Blacksmoke16> i'd also look into if you're making a CLI
<FromGitter> <Blacksmoke16> are a few shards out there that can make making CLIs easier as well if you need something more robust
<FromGitter> <> It there a case where `str == nil` wont work?
<FromGitter> <> The compiler won't be able to infer the type inside the condition?
<Na_Klar> I tried it and unless works and !str.nil? or .is_a? works, but if (str.nil? == false) does not work.
<FromGitter> <Blacksmoke16>
<FromGitter> <Blacksmoke16>
<FromGitter> <Blacksmoke16> prob the same reason why `str != nil` doesnt work as it turns some special type checking logic into simple boolean comparisons
<Na_Klar> I see. Okay, that one you have to know. Interpreters do not have that "problem".
<FromGitter> <Blacksmoke16> more so dynamically typed langs*
<Na_Klar> yes .. seems like the question if the renderer thinks "I have to know what will happen" or rather "let's see how it goes" :P
<FromGitter> <Blacksmoke16> pretty much yea
<Na_Klar> thx
<FromGitter> <> Dynamic languages uses basically assertions which can error at runtime. In crystal you can do it too with `not_nil!`, or `as(String)`
<FromGitter> <> However, to be avoided where possible of course
ur5us_ has joined #crystal-lang
<Na_Klar> yes, that makes sense. So three options: special method, error handling or forcing a pseudo type change.
<FromGitter> <Blacksmoke16> ehh `#.nil?`, `#try`, or `if var = xx` would be the way to go
<FromGitter> <Blacksmoke16> as mentioned, `#not_nil!` and `#as` aren't something you want to use majority of the time
<Na_Klar> I get that totally.
<Na_Klar> Just to evaluate my usecases: Is there a riscv64 platform support in the pipline? Or is that something rather not to count on with crystal?
<FromGitter> <tenebrousedge> idek how our Windows support is these days
<FromGitter> <Blacksmoke16> it would be accepted if someone wants to add it, but its not on the roadmap for the core team id say
<FromGitter> <Blacksmoke16> afaik to add new platforms, would just need a PR like
<Na_Klar> Would it help if I install a riscv board with a permanent UART pipeline for the community to develop on?
<FromGitter> <Blacksmoke16> i could see it being a platform
<Na_Klar> (meh, a virtual riscv architecture would do the job as well ..)
<FromGitter> <Blacksmoke16> so tl;dr, if you want to make a PR to support it im sure it would be welcomed, but i wouldnt plan on official/tier 1 support for it anytime soon
<Na_Klar> yes, that was my impression by the issue conversation as well. that's a pity. I personally can't even guess the efford to port a compiler to another platform ..
<FromGitter> <Blacksmoke16> afaik its *fairly* easy, just requiring some new compiler flags and bindings to lib c
<FromGitter> <Blacksmoke16> however given that would be a *totally* new arch, might be more to it ofc, as you'd first have to get the compiler working on it, prob via cross compiling it?
<Na_Klar> as I understood it, debian e.g. has an automatic port running for all the packages. I guess (guess!) that's how ruby got ported for riscv64. maybe that could work for crystal as well (but I don't have a clue tbo).
<FromGitter> <Blacksmoke16> LLVM handles the majority of that, whatever LLVM supports crystal could as well
<FromGitter> <Blacksmoke16> maybe in the future someone will take it on :P
notzmv has quit [Ping timeout: 252 seconds]
Na_Klar has left #crystal-lang [ - Komfortabler Chat. Überall.]
ox is now known as oz
ur5us_ has quit [Ping timeout: 240 seconds]
waleee has quit [Ping timeout: 240 seconds]
notzmv has joined #crystal-lang
ua_ has quit [Ping timeout: 272 seconds]
ua_ has joined #crystal-lang
ur5us_ has joined #crystal-lang
Guest78 has joined #crystal-lang
Guest78 has quit [Client Quit]
ur5us_ has quit [Remote host closed the connection]
ur5us_ has joined #crystal-lang
ur5us_ has quit [Ping timeout: 240 seconds]
<FromGitter> <rishavs> I am trying to initialize a class with a function as the input parameter. And not succeeding with it. Would appreciate some pointers on where I am going wrong ⏎
<FromGitter> <naqvis> `Proc` are invoked via `.call`. So you should be doing `` instead. And `return` keyword is redundant. Crystal returns the result of last executed expression
<FromGitter> <naqvis> also you are missing the type annotation for instance variable `@transformer_func`. Crystal is type safe language, remember ? 😄
<FromGitter> <rishavs> I am actually not sure how to write down the type for transformer_func. -_-
<FromGitter> <rishavs> Its a function. I don't know how to write the type signature of a function :(
<FromGitter> <naqvis> usage of `alias` is optional for your use-case, but same proc definition is used multiple times, its convenient to `alias` that.
<FromGitter> <rishavs> Thank you! I didn't know we could write function signatures like this `alias MyProc = String -> String`. learned something new
<FromGitter> <naqvis> You are welcome. `->` is just a syntactic sugar and you can write that as `Proc(String,String)` as well
<FromGitter> <naqvis> so ⏎ `alias MyProc = String -> String` is same as `alias MyProc = Proc(String,String)`
<FromGitter> <rishavs> Thanks! One more question. I am trying to do the same thing, but now with a function instead of a proc (for hopefully cleaner code) ⏎ ⏎ When I try to pass the function to the class initializer it is asking for arguments. How should I resolve this?
<FromGitter> <naqvis> There is hiccup here and Crystal expects you provide the arg type as well
<FromGitter> <naqvis> hopefully, in future such restriction will be removed, as compiler can already infer the type of arguments
<FromGitter> <rishavs> Thank you @naqvis ! 😄
<FromGitter> <naqvis> 🚀
hightower3 has joined #crystal-lang
ur5us_ has joined #crystal-lang
ur5us_ has quit [Ping timeout: 240 seconds]
Starfoxxes has quit [Ping timeout: 256 seconds]
hightower3 has quit [Read error: Connection reset by peer]
<FromGitter> <asterite> The reason why nil? will narrow types down and `foo == nil` won't is because technically you can redefine `==` so an object compared to nil is true, but the object itself isn't nil. I don't know if there's actually a use case for that, but if someone does that the code would segfault with those assumptions.
hightower3 has joined #crystal-lang
hightower3 has quit [Read error: Connection reset by peer]
brw has quit [Read error: Connection reset by peer]
brw has joined #crystal-lang
<FromGitter> <rishavs> If I have an Array(String) and I want to append either other arrays to it, or strings, how would I go about it? ⏎ I have tried this but hitting issues
<FromGitter> <tenebrousedge> it doesn't like when you try to use the array outside of the method, looks like
<FromGitter> <tenebrousedge> also note the difference between `Array(String).new` and `[] of Array(String)`
<FromGitter> <rishavs> oh. so is there no way to append two array in-place? because i want to run a for loop and append a lot of arrays to the `results` array
postmodern has quit [Remote host closed the connection]
<FromGitter> <rishavs> weirdly, this works
<FromGitter> <tenebrousedge> using `reduce` and `case ... in` is a bit cleaner, but yes, those have different scopes
<FromGitter> <tenebrousedge> you could also just call `flatten`
<FromGitter> <Blacksmoke16> you probably want to use
<FromGitter> <Blacksmoke16> `Array#+` returns a new array, while `Array#concat` adds them to/returns `self`
waleee has joined #crystal-lang
ejjfunky has quit [Remote host closed the connection]
<riza> is there an implementation of libgraphicsmagick in Crystal?
<FromGitter> <Blacksmoke16> not that i know of, but would have to search around
<riza> I poked around in a few places and didn't see anything for imagemagick or graphicsmagick
<FromGitter> <Blacksmoke16> guess you're out of luck then P
<riza> yeah I don't...think...I want to tackle native bindings for a library that big
ejjfunky has joined #crystal-lang
waleee has quit [Ping timeout: 252 seconds]
notzmv has quit [Ping timeout: 252 seconds]
Starfoxxes has joined #crystal-lang
waleee has joined #crystal-lang
olbat__ is now known as olbat
avane_ has quit [Ping timeout: 252 seconds]
avane has joined #crystal-lang
notzmv has joined #crystal-lang
ur5us_ has joined #crystal-lang
ur5us has joined #crystal-lang
ur5us_ has quit [Read error: Connection reset by peer]
ur5us has quit [Ping timeout: 240 seconds]
ur5us has joined #crystal-lang
ejjfunky has quit [Read error: Connection reset by peer]
ur5us has quit [Ping timeout: 240 seconds]
ejjfunky has joined #crystal-lang
wolfshappen has quit [Ping timeout: 256 seconds]
Na_Klar has joined #crystal-lang
<Na_Klar> I genuinely want to try to port crystal to riscv64. I thought about trying to cross-compile first, but that would mean to generate a new target with llvm and that is over my head. But I saw that the first crystal compiler was written in ruby, and since I have a riscv board with a working ruby I could go the long way and compile all versions of the compile till the current from the scratch. I have been looking for it but didn't find it
<Na_Klar> , where can I get the first compiler written in ruby?
<FromGitter> <Blacksmoke16> ultimately it takes a non-trivial amount of memory to build the compiler, so if you dont have like 4-6 gigs might be out of luck
<Na_Klar> uh that's a problem ..
<FromGitter> <Blacksmoke16> plug in a USB and use it as swap? 😅
<Na_Klar> for swapping is enough space, but that's gonna be slooow ..
<FromGitter> <Blacksmoke16> not like it would have been faster in the first place
<FromGitter> <Blacksmoke16> been fast*
<Na_Klar> that makes it even worse xD
<riza> walking from bootstrapped ruby all the way to modern crystal is a tour de force
<Na_Klar> but whatever. time is no issue.
<Na_Klar> I just hope I don't have to downgrade the system at every step ..
<FromGitter> <Blacksmoke16> afaik the script should handle all that
<FromGitter> <Blacksmoke16> granted i never ran it/went down this road before tho
<Na_Klar> ah I see .. that would be helpful indeed
<Na_Klar> in case I succeed: should I give the latest compiled version back to the community? if so, where do I call? (optimism activated)
<FromGitter> <Blacksmoke16> afaik you'd need to make a PR like one of the two i linked the other day
<FromGitter> <Blacksmoke16> i.e. setting up the libc bindings for stuff the stdlib uses
antoszka has quit [Read error: Connection reset by peer]
r0bby has quit [Ping timeout: 260 seconds]
kevinsjoberg has quit [Read error: Connection reset by peer]
pusewic|away__ has quit [Read error: Connection reset by peer]
kevinsjoberg has joined #crystal-lang
Liothen has quit [Read error: Connection reset by peer]
kiwiroy has quit [Read error: Connection reset by peer]
Liothen has joined #crystal-lang
antoszka has joined #crystal-lang
kiwiroy has joined #crystal-lang
pusewic|away__ has joined #crystal-lang
r0bby has joined #crystal-lang
<Na_Klar> I see .. thx