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
<FromGitter> <tenebrousedge> Crystal has no runtime metaprogramming
<FromGitter> <tenebrousedge> I don't know for sure if you can parse the YAML at compile time, doing that at runtime may be easier
<FromGitter> <riffraff169> so you define a macro, then in your code when you do: `mymacro()`, then that call only executes when compiling, so that specific line isnt included in the final binary....ok....
<FromGitter> <riffraff169> just the results of the macro are included
<FromGitter> <tenebrousedge> yes
<FromGitter> <riffraff169> ok, lets see if i can figure out what i need to do, thanks
<FromGitter> <riffraff169> is it useful to define a macro on a module, or not
<FromGitter> <tenebrousedge> sure
<FromGitter> <riffraff169> ok
<FromGitter> <Blacksmoke16> fwiw you can do `{{read_file "data.yml}}`
<FromGitter> <Blacksmoke16> which would read the contents of the file into the binary, which could be done in a lazy class getter like `class_getter : Data { Data.from_yaml {{read_file "data.yml}}`
<FromGitter> <Blacksmoke16> ideally design is so that if you ever want to have it backed by a db or something it would be fairly easy to do
mteverst has quit [Quit: away]
<FromGitter> <riffraff169> interesting
<FromGitter> <riffraff169> so, how to `read_file` from a var that is a macro var: `{{ read_file "#{ {{ var }} }" }}` ....doesnt seem to work....no nesting?
<FromGitter> <riffraff169> or `{{ read_file var }}`
<FromGitter> <Blacksmoke16> does the latter not work?
<FromGitter> <riffraff169> but if i need to append two vars, like a path name: `{{ read_file var1 + var2 }}` ?
<FromGitter> <Blacksmoke16> whats the error?
<FromGitter> <riffraff169> im experimenting,
<FromGitter> <riffraff169> originally was this until i figured that out: ` > 1 | MYHASH = Hash(String,Activity).from_yaml(read_file "#{ "../data" }/#{ "activity.yml" }" )`
<FromGitter> <riffraff169> that didnt work because read_file is not a top level function
<FromGitter> <riffraff169> macro:
<FromGitter> <riffraff169> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=60876167a05f8b582a0c32ae]
<FromGitter> <riffraff169> basically i want `read_file("#{datadir}/#{file}")`
<FromGitter> <riffraff169> however the macro would be though
<FromGitter> <riffraff169> that gave me: `Error: undefined macro method 'StringLiteral#/'`
<FromGitter> <Blacksmoke16> `read_file "#{datadir}/#{file}"`?
<FromGitter> <riffraff169> thats better, but calling with this: `make_data(MYHASH,Activity,"../data","activity.yml")`
hendursa1 has quit [Quit: hendursa1]
<FromGitter> <riffraff169> gave me this: `Error: Error opening file with mode 'r': '\"../data\"/\"activity.yml\"': No such file or directory`
<FromGitter> <riffraff169> like i need to do some weird escaping there or something?
<FromGitter> <Blacksmoke16> `read_file "#{datadir.id}/#{file.id}"`
hendursaga has joined #crystal-lang
<FromGitter> <riffraff169> or not have the quotes when passing...hmm
<FromGitter> <riffraff169> ok, hmm
<FromGitter> <riffraff169> awesome...so what is the difference, technically, between datadir and datadir.id?
<FromGitter> <Blacksmoke16> basically makes it so whatever you pass it, its the same underlying value
<FromGitter> <riffraff169> interesting....cool, you learn something new every day
<FromGitter> <riffraff169> thanks for the help, going to do some testing now and see how it all works!
theman100 has joined #crystal-lang
<theman100> Hi guys. Crystal supports full async?
<FromGitter> <naqvis> theman100 there are no `async`, `await` keywords in Crystal, but you can achieve that via `Fiber` and `Channel`
<theman100> I see. any docs you can supply me with?
<theman100> thanks :)
<FromGitter> <naqvis> 👍
chachasmooth has quit [Ping timeout: 252 seconds]
chachasmooth has joined #crystal-lang
theman100 has quit [Quit: Connection closed]
ua has quit [Read error: Connection reset by peer]
ua has joined #crystal-lang
<FromGitter> <robear> I have a Kemal question - is there a way to pass variables to the layout? eg. render "src/views/index.ecr", "src/views/layouts/front.ecr" so that front.ecr sees variables like index.ecr does?
_ht has joined #crystal-lang
<FromGitter> <naqvis> `render` in Kemal is just a macro, so what prohibits you from either overriding that? or simply you can have the variables in scope before you invoke `render` method and try to access them from template
hendursaga has quit [Ping timeout: 240 seconds]
hendursaga has joined #crystal-lang
sz0 has joined #crystal-lang
kevinsjoberg has left #crystal-lang ["Textual IRC Client: www.textualapp.com"]
hendursaga has quit [Ping timeout: 240 seconds]
sagax has joined #crystal-lang
hendursaga has joined #crystal-lang
richbridger has joined #crystal-lang
ua has quit [Ping timeout: 252 seconds]
ua has joined #crystal-lang
loiajdawi has joined #crystal-lang
<loiajdawi> hi guys, how do I throw an error if function parameter is not a number?
<FromGitter> <tenebrousedge> type it
<FromGitter> <Blacksmoke16> prob would be better to add a type restriction
<FromGitter> <tenebrousedge> ^
<loiajdawi> Thanks
<loiajdawi> so `def whatever(whatever : Number)` should be enough? handles errors and all alone?
<FromGitter> <Blacksmoke16> i mean it would make the code not compile if someone tried to do like `whatever false`
<FromGitter> <tenebrousedge> you may want to use something more restrictive than `Number`
<FromGitter> <Blacksmoke16> is that what you mean by "handles errors"?
<loiajdawi> sorry, I'm new to crystal, so might be not totally aware how stuff is handled yet properly
<loiajdawi> but yes if the user gives a string should it not let the code run?
<loiajdawi> or anything else than an actual number
<FromGitter> <Blacksmoke16> the wouldnt even compile
<FromGitter> <Blacksmoke16> i.e. never run in the first place
<loiajdawi> i see
<FromGitter> <Blacksmoke16> but assuming all you want to do is ensure that value is a `Number` thats all you have to do
<FromGitter> <Blacksmoke16> it doesnt apply additional logic to it, like is the number too big/small, or even/odd or whatever you need it to do
<FromGitter> <tenebrousedge> if you use `Number`, you should probably make sure that your code works with both floats and ints
<loiajdawi> yeah, I understand. just have to make sure it's a number. later in the function I can add if statements, etc doing whatever with it, correct?
<FromGitter> <Blacksmoke16> and `Big*` types
<FromGitter> <erdnaxeli:cervoi.se> loiajdawi, the compiler makes sure for you that it is a number (or any type that inherites from number). Then you can use any method that are avaiable on Number.
<loiajdawi> I see. thanks
<FromGitter> <robear> @naqvis it was a PEBKAC error. I had an action that used the layout but didn't have the variable and it wasn't what I was working on and it slipped my mind and that is where the error might be coming from. Thanks for your patience.
<FromGitter> <tenebrousedge> sometimes the language itself has bugs, but I find that most often, and particularly my most troublesome bugs were PEBKAC: an incomplete understanding of the problem
<FromGitter> <tenebrousedge> I tend to think of coding as an exploration of what I don't know about coding, and of the problem I'm solving
<FromGitter> <naqvis> that happen to all of us :P
loiajdawi has quit [Quit: Connection closed]
<FromGitter> <robear> @tenebrousedge yeah, and coming from a dynamic language, you are used to only what you are executing to be evaluated, not the entire application. The advantage of Crystal, obviously, is that it is compiled and the whole application is checked. It is just a bit of a mental shift in what to look for when you have issues.
lol29k has joined #crystal-lang
<lol29k> when writing a crystal shard is the `spec` directory necessary or can it be removed?
<FromGitter> <Blacksmoke16> you should ideally write some specs yes :) but no it's not required
<lol29k> what are they used for ?
<FromGitter> <Blacksmoke16> that your code works correctly
<lol29k> thank you
<lol29k> and how do I publish the module after? to shards website
<FromGitter> <Blacksmoke16> shards website?
<lol29k> crystalshards.xyz
<FromGitter> <Blacksmoke16> that just aggregates shards to show you whats out there. It doesnt have anything to do with installing shards
<FromGitter> <Blacksmoke16> it just scapes github for repos with the `crystal` tag afaik
<lol29k> oh, I see. sorry, I misunderstood then. thanks for letting me know.
<FromGitter> <Blacksmoke16> there isnt a central repo of shards like `npm` for example. They're installed by just cloning the related repo
<lol29k> all good :]
<lol29k> what is the difference between `def x (x)` and `def x (@@x)`
<lol29k> and what is recommended?
<lol29k> they both seem to be the exact same, except for syntax.
<FromGitter> <Blacksmoke16> `@@x` denotes a class variable
<FromGitter> <tenebrousedge> `@@var` is probably something to avoid unless you are quite sure that is exactly your desired behavior
<FromGitter> <Blacksmoke16> https://play.crystal-lang.org/#/r/b03g
<FromGitter> <tenebrousedge> but you will often see `def initialize(@var, @other)`
<FromGitter> <Blacksmoke16> shortcut to doing like ⏎ ⏎ ```def x(x) ⏎ @@x = x ⏎ end``` [https://gitter.im/crystal-lang/crystal?at=608844acc1a9210b3c24517f]
<FromGitter> <Blacksmoke16> but thats prob not what you're actually wanting...
<lol29k> I see
<FromGitter> <Blacksmoke16> 👍 could prob help more if you share some more code
<lol29k> figured it, thank you for providing me with the information.
<FromGitter> <tenebrousedge> just give the information back when you're done 😉
<lol29k> well, it was really nothing. was just curious to see if that's another way to declare method params
<FromGitter> <Blacksmoke16> tl;dr yes, but with special meaning
<FromGitter> <RespiteSage> Re shard websites, while it's not a central repo, you would have to add your shard to https://github.com/shardbox/catalog if you want it to show up on shardbox.org.
<lol29k> just a pull request?
<FromGitter> <RespiteSage> I believe so, yes.
<lol29k> understood.
<lol29k> this reminds me on how deno used to handle third party modules
<FromGitter> <RespiteSage> I've not yet added my stuff myself, but I also barely have any useful Crystal publicly available.
<FromGitter> <Blacksmoke16> and again it doesnt have anything to do with installing/etc. Just acts as a way to discover shards
<lol29k> yeah, they're served from github/gitlab. I get that part
<FromGitter> <Blacksmoke16> so its not like you *need* to add everything you make to it
<FromGitter> <Blacksmoke16> 👍
<lol29k> neat way of handling modules I think
<lol29k> well anyways, thanks guys. helped a lot
lol29k has quit [Quit: Connection closed]
<FromGitter> <riffraff169> okay, i have a macro that can take a string, but the string can be either a `StringLiteral` or a `String`:
<FromGitter> <riffraff169> `mymacro "file"` or `file = "test.txt"; mymacro file`
<FromGitter> <Blacksmoke16> a macro cant take a `String`
<FromGitter> <Blacksmoke16> because thats a runtime type
<FromGitter> <Blacksmoke16> i.e. you cant use local vars with a macro
<FromGitter> <riffraff169> so i cant set a var and use that var, it has to be raw data
<FromGitter> <riffraff169> ok
<jhass> you can pass the var and work with it (reference it in generated code), but not with the value
<FromGitter> <Blacksmoke16> it can be a macro var, or a const, or from an annotation
<FromGitter> <Blacksmoke16> just not local vars or class vars etc
<FromGitter> <riffraff169> hmmmm....since im really concatenating a path, directory + file, i might as well just pass it combined into one string...
<FromGitter> <riffraff169> thanks
maxpowa has quit [Ping timeout: 265 seconds]
maxpowa has joined #crystal-lang
<FromGitter> <RespiteSage> @oprypin Your Crystal CI configuration web page is a wonderful tool.
<FromGitter> <oprypin:matrix.org> thanks 😊
DTZUZU has quit [Read error: Connection reset by peer]
jimmyl28 has joined #crystal-lang
_ht has quit [Remote host closed the connection]
DTZUZU has joined #crystal-lang
<jimmyl28> can someone assist with socket lib?
<jimmyl28> i'm trying to read in a non blocking way
<straight-shoota> IO is non-blocking by default
<straight-shoota> what's your problem?
<jimmyl28> i see. never mind then. what would be the recommended way to read the entire response sent from the server? currently im using `gets_to_end`
<straight-shoota> yeah, that's probably okay
<straight-shoota> might not be a bad idea to add some limit, though
<jimmyl28> i can send my sample code right now
<jimmyl28> paste.debian.net/plainh/7e6cc398
<jimmyl28> do you have any suggestions?
jimmyl28 has quit [Ping timeout: 240 seconds]
ua has quit [Ping timeout: 252 seconds]
avane has quit [Ping timeout: 252 seconds]
ua has joined #crystal-lang
avane has joined #crystal-lang
PriceMight has joined #crystal-lang
<PriceMight> how do i loop through something without "blocking" the loop? if you want me to be more specific: once in the loop i have to call a single method several times, but it first waits for one method call to finish, then goes on, till loop end. any way to make it not do that and run them all at once?
<FromGitter> <oprypin:matrix.org> PriceMight, `spawn` instead
<PriceMight> what about anonymous methods? are they existant?
<PriceMight> aa i see ,thanks