<trapped>
this time it's something about the json mapping or something
<trapped>
sorry if i keep asking but i'm clueless as there's no docs about the json module
<jhass>
trapped: String? is shortcut notation for String|Nil, so you're passing a potential nil to the parser, which doesn't like that
<jhass>
trapped: data = Request.from_json request.body.not_nil! try that
blue_deref has joined #crystal-lang
<jhass>
(note it'll probably crash if someone sends you some request without a body, so better handle the nil case if that fixes it
<jhass>
)
<trapped>
the compiler error is longer than my console max lines
<nopc0de>
HTTP request body.to_s
<nopc0de>
never nils
<nopc0de>
i think
<nopc0de>
I'm using request.body.to_s
<trapped>
updated
<jhass>
nopc0de: that'll just cause the exception to be a json parse error for an empty body then ;)
<trapped>
same thousand-line error with to_s
<crystal-gh>
[crystal] PragTob opened pull request #1136: Improve documentation of struct in regards when to use it (gh-pages...when-to-use-struct) http://git.io/v3Icd
<nopc0de>
jhass Right, I only tested form data
<jhass>
trapped: you'll need to provide a specific Hash, Hash(String, String) or whatever
<BlaXpirit>
jhass, to send DBUS variant type which may contain DBUS struct type
<BlaXpirit>
i mean, this doesn't have to be tuple, but i don't think there is a way to make a generic that accepts an arbitrary number of types
<jhass>
maybe you could be a tad more specific, like some explanation that doesn't require an hour of reading docs and guessing what kind of solution came to your mind? ;)
<BlaXpirit>
jhass, how i'm gonna try to explain depends on whether you know DBus
<jhass>
I know what it is for, I didn't work with the protocol directly
<jhass>
libnotify is about as deep as I got really
<BlaXpirit>
jhass, well you can call some "methods" and send various data types to them
<BlaXpirit>
if I send a string, i can check is_a? String, then send the corresponding enum member and pointer to string
<BlaXpirit>
if I send an Array(String), i can check is_a? Array and (by adding a method to Array) get the type of the array
<BlaXpirit>
of the array's member, that is
<jhass>
typeof(a[0]) works too
<BlaXpirit>
problem is, i can't do much with this type object. i can't check what it is
<crystal-gh>
[crystal] PragTob opened pull request #1138: Use an up to date ruby version for gh-pages (gh-pages...update-ruby-version) http://git.io/v3IRd
<BlaXpirit>
jhass, that's compiletime
<jhass>
so you have huge ass unions?
<BlaXpirit>
uh pretty much
<BlaXpirit>
because there is this type Variant
<BlaXpirit>
which means any type can be sent
<jhass>
but I mean the interface is sort of defined, no?
<BlaXpirit>
jhass, everything is free to define its own methods based on dbus api
<jhass>
iirc introspectable
<BlaXpirit>
introspectable, but variant is just variant
<jhass>
yeah, but I mean for concrete APIs you're going to call
<BlaXpirit>
i was almost done with my introspecting solution, then the goddamn variant ruined everything
<jhass>
are there really methods that take "whatever"?
<BlaXpirit>
for example, (to translate to crystal's terms) there is a method that (among other things) accepts Array(Hash(String, Variant)))
<jhass>
my first intuition would be to ponder another code generation approach, generate specific classes with casts, type restrictions and whatever for specific APIs
<BlaXpirit>
and Variant could be anything, even another damn Array(Hash(String, Variant))
<jhass>
that sucks
<jhass>
how is a Variant serialized?
<BlaXpirit>
jhass, i can't go with specific apis, because the possibilities are endless, anyone can define another method and it's immediately not supported by my lib
<jhass>
well, the support would "here, run this generator for your API"
kyrylo_ has joined #crystal-lang
NeverDie has joined #crystal-lang
<jhass>
so Variant is C union I guess?
<BlaXpirit>
jhass, there is this whole protocol that i dont know how to use. but to send a variant you say "starting to send a variant", then you give a string signature like "a{si}" which is Array(Hash(String, Int32)), then, because it's an array, you say "i'mstarting to send an array" then you send first item "i'm staring to send a hash of these things" BLAH BLAH RECURSIVITY i'm finishing an array i'm finishing a hash
<BlaXpirit>
u dont need to know the protocol, u just use the lib
kyrylo has quit [Ping timeout: 246 seconds]
<trapped>
another question: how am i supposed to use if/each-do/etc blocks in ECR files?
<jhass>
trapped: do you know ERB?
<trapped>
a bit, yes
<jhass>
like that
<jhass>
<% crystal code %> template template <%= crystal code which's value will be inserted %>
<trapped>
it complains if i do `<% if something %> something <% end %>
<trapped>
`
<BlaXpirit>
so since the types are limited to basically Type = Bool | Int16 | UInt16 | Int32 | UInt32 | Int64 | UInt64 | Float64 | String | Array(Type) | Hash(Type, Type) | Tuple(Type x N) (yeah, that final part is invalid), i think string parsing will work
<jhass>
did you try turning it off and on again?
<jhass>
(= default response to "didn't work" type of problem descriptions)
<jhass>
BlaXpirit: meh
<trapped>
it complains a bit less if i do `<% if something { %> something <% } end %>
<jhass>
BlaXpirit: I guess a big case when dispatching to handler methods for each type and some recursivity could work
<trapped>
but it still complains if i do <% ary.each do { |e| %> <%= e %> <% } end %>
<jhass>
BlaXpirit: well, or relying on type restrictions even
<BlaXpirit>
jhass, i've spent so many days trying so many things, but there is just no way
<willl>
trapped: if you use do..end you dont need { }
<willl>
one or the other
<trapped>
`expecting token '=>', not 'NEWLINE'`
<trapped>
uhh
<BlaXpirit>
jhass, relying on type restrictions stops working as soon as an empty array is encountered
<BlaXpirit>
and : T.class restrictions just do not work except for trivial types
<BlaXpirit>
jhass, i think this fails on empty arrays
<jhass>
mmh
<BlaXpirit>
i've been there
<jhass>
well, I see no way beyond having it explicit then, send(type : String, data : Variant)
<jhass>
and a generator using introspection that wraps that into classes for specific APIs
<BlaXpirit>
jhass, i'd rather parse type string, it works for sure :p
<jhass>
meh
<BlaXpirit>
jhass, and won't be needed if variants are not used, and perhaps i could specialcase if a trivial type is sent into a variant
<BlaXpirit>
(you seem to have misunderstood me here, because "type" and "variant" are different things. variant is a type that means "any type can be put in here")
<BlaXpirit>
thanks
<nopc0de>
Is there support for multipart/form-data in crystal?
dbackeus has quit [Read error: Connection reset by peer]
dbackeus has joined #crystal-lang
ssvb has quit [Quit: Leaving]
<BlaXpirit>
I have some Python code, and I would love to be able to write something like this in Crystal. I can't free myself from Python's paradigms. the main problem I have is with the "yield" ing. in Python the function that yields just spits out values one by one, and the actual control is given to the function that uses it.
<BlaXpirit>
in Crystal, however, the function that yields is basically the dominating thing, and it manages the calling of the block given to it. i just can't make myself think this way
<BlaXpirit>
i would be forever in debt if someone explained me how to do something similar in Crystal
<jamie_ca>
BlaXpirit: I have no idea if this description is relevant to Crystal, but in Ruby...
<jamie_ca>
a method has an implicit block (I know, Python is all about preferring explicit)
<jamie_ca>
so in ruby, I'd write def tokens_generator(&block)
<jamie_ca>
if I wanted to use it
<jamie_ca>
and then explicitly using it, inside the method instead of yield "{", I would instead block.call("{")
<jamie_ca>
as though it were a coroutine
<BlaXpirit>
jamie_ca, do you know Python or understand what i'm doing with next()
<BlaXpirit>
this is not normal iteration
<jamie_ca>
BlaXpirit: you're taking your generator function, and converting it to an iterator, which uses next to grab the next yielded value?
<BlaXpirit>
jamie_ca, that sounds right
<jamie_ca>
actually, I could probably do that generator -> iterator conversion in ruby, but I'm probably better off not guessing about the crystal internals to do so
<BlaXpirit>
well i could start with ruby then
<BlaXpirit>
Enumerator looks like exactly what i want, but there is no such thing in Crystal
<BlaXpirit>
it is obvious that this is not meant to be done in these languages, i need a change of paradigms, but i dont understand
<jamie_ca>
yeah, some voodoo with Enumerator in ruby, for sure
<jamie_ca>
BlaXpirit: ok, I think I see what you're doing...
<jamie_ca>
tokens_to_signature, in Ruby, I would assemble a string to return rather than yielding each output character, and that gets rid of some of the indirection
<BlaXpirit>
jamie_ca, not a problem in this case, but sometimes i would want lazy computation. but it's ok if we skip this for now
<jamie_ca>
BlaXpirit: I need to leave shortly for the afternoon (it's just past 12 here) - will you be around this evening?
<BlaXpirit>
jamie_ca, it's 22 here
<jamie_ca>
ahh
<jamie_ca>
(like, in 8h)
<BlaXpirit>
will be here 3 more hours max, so no, sorry
<BlaXpirit>
but then i will be available all the time 12 hours from now
<jamie_ca>
I can totally chat you up tomorrow this time though
<BlaXpirit>
great, thanks a lot. i will remind you (unless i already have the solution)
<jamie_ca>
sure
<BlaXpirit>
can't define def inside def :S
<jhass>
BlaXpirit: yield in Crystal/Ruby is not for generators, that's what Enumerator in Ruby and Iterator in Crystal is for
<BlaXpirit>
jhass, sure, now tell me how I would implement tokens_generator using Iterator
<jhass>
Ruby allows to easily generate Enumerator's from a method that yields multiple times, but Ary found it to be too slow
<BlaXpirit>
literally this function - how i would implement it
<BlaXpirit>
i mean,... obviously this could be put in a @array and incrementing @index
<BlaXpirit>
so nevermind, jhass, this example is easy to game
<BlaXpirit>
oh so it doesn't work like that and you must implement both a function that takes a block and one that returns an iterator :(
<jhass>
yes
<BlaXpirit>
python is way superior in this department
<BlaXpirit>
you pretty much just get an iterator for free
strcmp1 has joined #crystal-lang
<BlaXpirit>
(yes, it's slow)
<jhass>
because you don't get a block
<BlaXpirit>
yes
<jhass>
we could do the same as Ruby, where you actually get both for free with just one loc, as my PoC shows, but as said it's not fast enough
<BlaXpirit>
not fast enough for stdlib
<jhass>
I tried to persuade Ary to include (something like) Enumerator into stdlib and so you can default to it and write optimized Iterator's when you have a bottle neck
<jhass>
but he didn't like it
<BlaXpirit>
but crystal is fast enough that many people would not care for a small slowdown in one area for huge convenience
<jhass>
I guess we can make enumerator just a library though
<BlaXpirit>
i can only wonder whether compiler support could make it better
<BlaXpirit>
it could make it look better, that's one thing
<jhass>
you'll need coroutines and that's why it's slow
<jhass>
same in Ruby, it's done with fibers there too
<jhass>
and I can't imagine Python does it differently
<jhass>
after all you need to jump between stack frames
<BlaXpirit>
python uses coroutines, yes, and i love it for the convenience with coroutines
<BlaXpirit>
yes, thats' what happens
<BlaXpirit>
i pretty much just want coroutines in crystal and i'm set for life
<jhass>
uhm
<jhass>
spawn is coroutines
<sfcgeorge>
Well optimized std lib sounds best to me, but I like jhass idea of Enumerator class for convenience in client code.
<BlaXpirit>
no mention about spawn in docs
<jhass>
I guess they keep it undocumented for now in case they need to introduce syntax to make it work with a multi threaded scheduler