<FromGitter>
<Blacksmoke16> cant really have code that does stuff when required like that
<FromGitter>
<Blacksmoke16> use two files, one that defines the lib code and another as an entrypoint into the CLI
<SamantazFox>
ok, I found out my problem
<SamantazFox>
I had to require `protodec/utils`
<SamantazFox>
but the bug is weird nonetheless
<FromGitter>
<Blacksmoke16> not really
<FromGitter>
<Blacksmoke16> you have code in the top level that is doing `STDIN.gets_to_end`, you're requiring that in your code, and when you run tests there is no STDIN so it blocks waiting for input
<SamantazFox>
waiting for input? what input?
<FromGitter>
<Blacksmoke16> yea
<FromGitter>
<Blacksmoke16> just run like `crystal eval 'pp STDIN.gets_to_end'`
<FromGitter>
<Blacksmoke16> any input
<SamantazFox>
I mean, why is it waiting for an input? Shouldn't that raise a compile-time error?
<FromGitter>
<Blacksmoke16> how would it raise a compile time error?
<FromGitter>
<Blacksmoke16> STDIN is a runtime thing
<FromGitter>
<Blacksmoke16> and its waiting because you're telling it to get all the data from that IO, but there is none, so it waits
<SamantazFox>
Oh, okay, I see
<SamantazFox>
but in my case, I don't see why it hangs like that?
<SamantazFox>
the main file of protodec requires the `utils` file
<FromGitter>
<Blacksmoke16> right, but all that code to parse data from stdin is on the top level of the main file, so it also runs when its required
<FromGitter>
<Blacksmoke16> or at some point when you run the file that requires it*
<SamantazFox>
Ok, I thought that the `#to_json` method of a `Hash` was standard (i.e not required to `require` something else)
<FromGitter>
<Blacksmoke16> no you need to require json module to get that default to/from json logic
<SamantazFox>
ok
<SamantazFox>
I'd like the compiler to be smarter sometimes ^^>
<FromGitter>
<Blacksmoke16> smarter how? suggest to require that module?
<SamantazFox>
yeah, something like that
<FromGitter>
<Blacksmoke16> i feel like that would be verryyyyyy tricky
<FromGitter>
<Blacksmoke16> as for 1 it wouldnt know what code is in it if you dont require it, plus handling shards/overloads etc
<SamantazFox>
like "hmm, `to_json` on a <stdlib type>? maybe they forgot that module"
<SamantazFox>
I don't ask something like Rust (their compiler messages are both awefully and incredibly detailed) but maybe some basic logic?
<SamantazFox>
(I've never looked at the compiler code, so maybe that's completely unfeasible)
<FromGitter>
<Blacksmoke16> but what if another shard also defines `to_json`
<SamantazFox>
Well then the error saying that there is no method for <stdlib type> won't be raised
<FromGitter>
<Blacksmoke16> but how would it know what methods even are available if its not required
<FromGitter>
<Blacksmoke16> idk its an idea, maybe start a forum thread about it and go from there?
<SamantazFox>
to/from json/XML are, afaik, the few methods that aren't part of the base type definition (which are always "required"). So it could be easy to check those know cases (of course, it would be impossible to "browse" non-required shards)
ur5us has joined #crystal-lang
<SamantazFox>
As for the forum thread, yeah, that's a good idea
<SamantazFox>
will do that (I need to make an account first, tho)
<FromGitter>
<asterite> I think if these are very common issues people stumble upon, I wouldn't mind adding that to the standard library. Same for YAML. And even...
<FromGitter>
<asterite> I think the second one we should definitely do. When you do `require "json"` it defines a `to_json` for all objects, but it only works if you define a `to_json(builder : JSON::Builder)` for a given object. Without that guidance you will probably be lost unless you are familiar with how things work.
<FromGitter>
<asterite> Feel free to suggest these things in the forums, github, etc.!
<FromGitter>
<asterite> I wonder if Crystal is the only language that lets you produce customized compiler errors... 🤔
<FromGitter>
<asterite> Maybe not, it seems Scala also does it
<SamantazFox>
asterite: yeah, that's not the way I was expecting it, but that's perfect :D
<SamantazFox>
That would honnestly help *a lot*
<SamantazFox>
I'm pretty sure it's the kind of basic errors that happen all the time.
<FromGitter>
<asterite> I might even open a forum thread about this tomorrow. It might look a bit hacky (why is there a `to_json` method with a custom error message there?) but Crystal is all about being pragmatic, and these kind of errors will be super useful to have
<SamantazFox>
I don't see how it's hacky
<FromGitter>
<asterite> Well, as George said, if you don't use the standard json library and something else, that error will be misleading. But chances of that happening are low or null in my opinion
<SamantazFox>
asterite: In the even where someone uses some other library that declares `#to_json`, then only the "require" name will be wrong. It would still hint the user about a missing `require`.
<FromGitter>
<Blacksmoke16> I'm sure you could word craft the message to use it as an example in a more general question
Elouin has quit [Quit: Ping timeout (120 seconds)]