News for hackers. Stuff that matters.
Re:Reducing Memory Usage in Ruby by eropple on Tuesday January 23, 2018 @08:57PM (#16219720)
Until I can perform runtime define_method calls and dynamically construct classes, it doesn't matter what Crystal does. It's fundamentally antithetical to my needs. (JavaScript isn't, but JavaScript is kind of clunky.)

I have Kotlin, C#, C++, and Rust if I need static typing. Ruby is for when I don't.

Re:Reducing Memory Usage in Ruby by sam0x17 on Tuesday January 23, 2018 @10:30PM (#16220090)
People think "oh, it's compiled, so I can't have dynamic methods" but that couldn't be more far from the truth. You can do all of that at compile time, and Crystal provides an excellent way of doing so. E.g.: https://github.com/sam0x17/crystal-mongo-orm/blob/master/src...
Re:Reducing Memory Usage in Ruby by yorwba on Tuesday January 23, 2018 @09:13PM (#16219784)
How late at runtime do these dynamic definitions need to be possible? Just at startup or also after the program has been running for a while?

I occasionally wonder whether it might be possible to accommodate more dynamic language features with a compiler that treats them like a macro system that is expanded statically, such that no actual dynamicism is necessary at runtime.

Re:Reducing Memory Usage in Ruby by sam0x17 on Tuesday January 23, 2018 @10:30PM (#16220093)
This is exactly what crystal does
Re:Reducing Memory Usage in Ruby by notalex on Wednesday January 24, 2018 @10:02AM (#16223175)
Reading well written source code creates a more efficient workflow than reading api docs. For me, the fact that Crystal is written in Crystal gives it a far greater advantage.

Plus it is faster than Go/Java/Elixir. As others have pointed out, Crystal does have meta programming capabilities.

Re:Reducing Memory Usage in Ruby by sam0x17 on Tuesday January 23, 2018 @10:28PM (#16220079)
You can do all of that with really easy to write crystal macros.
Re:Reducing Memory Usage in Ruby by eropple on Tuesday January 23, 2018 @10:37PM (#16220111)
You can dynamically build at runtime an HTTP client from an OpenAPI spec (unavailable until runtime, to be clear), method-by-method, using macros? Tell me more.
Re:Reducing Memory Usage in Ruby by look_lookatme on Wednesday January 24, 2018 @07:57AM (#16222313)
Yeah use Ruby. I prefer to have well defined interfaces for the objects I use. I love Ruby to death, but it's frustrating to encounter code like this:

  [:thing, :other_thing].each do |msg|
    define_method(msg) do
      @json[msg].to_s
    end
  end
When this is far more clear and, for me, aesthetically pleasing:

  def thing
    @json[:thing].to_s
  end

  def other_thing
    @json[:other_thing].to_s
  end
I'm not sure how you'd use a ruby object that maps to an OpenAPI spec if you don't know what's in the API response until runtime... surely you or the user of your library has an idea what's in the API they are consuming?
Re:Reducing Memory Usage in Ruby by eropple on Wednesday January 24, 2018 @12:17PM (#16224343)
You may have an idea, sure, but the alternative is to codegen a new instance of the client library any time anything changes rather than keeping up to date with the API as it evolves and exists. (The moral equivalent is pulling a WSDL during app startup and making sure that you're presenting a correct interface to it.)

I full-stop don't do codegen in any project under any circumstance that is not done strictly within the workflow of a build tool (i.e., a Maven generated-sources gizmo). OpenAPI's options are pretty much all exactly not that: all the codegen options's happy paths seem to be vendoring generated code. And that makes me itch, too.

Re:Reducing Memory Usage in Ruby by yxhuvud on Wednesday January 24, 2018 @11:22AM (#16223896)
The example in question is perfectly possible to do in Crystal during compile-time, using macros. What isn't possible is to create a new class on the fly as a sort of magic lookup table that magic methods is called upon. Good riddance, for that functionality.
Re:Reducing Memory Usage in Ruby by look_lookatme on Friday January 26, 2018 @03:57PM (#16242846)
For the sake of this example, consider [:thing, :other_thing] to be dynamically created at runtime.
Re:Reducing Memory Usage in Ruby by papaf on Wednesday January 24, 2018 @01:10AM (#16220713)
Although it probably does not fit your use case something like Common Lisp would shine in a situation like this.

You can build your code at runtime from the spec and then compile it at runtime to get pretty good performance.

Re:Reducing Memory Usage in Ruby by eropple on Wednesday January 24, 2018 @01:52AM (#16220876)
Sure. Or I get effectively the same thing with JavaScript. (I just don't much like Node--it's not bad but it's not great--and the dev experience for specifying something like a DSL is a bummer.)
Re:Reducing Memory Usage in Ruby by sam0x17 on Wednesday January 24, 2018 @12:27AM (#16220544)
Got me there. First time I've ever in my life heard of a legitimate example where you really do need dynamic methods.
Re:Reducing Memory Usage in Ruby by eropple on Wednesday January 24, 2018 @12:42AM (#16220599)
If you're doing stuff with OpenAPI (as I've been doing over the last few weeks), it becomes a pressing thing. =( Generated clients usually are pretty awful, you usually have to generate your own (because they don't publish gems when their APIs change, which is why I've written online-generated ones!), and once you've completed the bootstrap process for an HTTP client, it's as fast as any other Ruby thing. Which is not the fastest out there by a long shot--though JRuby helps--but is Fast Enough for most stuff.

I'm also not sure Crystal can implement an active record pattern off of a database? (Though I am not personally the type to go with that, I prefer row mappers.)

Re:Reducing Memory Usage in Ruby by sam0x17 on Wednesday January 24, 2018 @02:37AM (#16221047)
See GraniteORM or my crystal-mongo-orm. You just have to specify your fields at compile time and you are good-to-go. It works just like active record.

https://github.com/amberframework/granite-orm https://github.com/sam0x17/crystal-mongo-orm

Re:Reducing Memory Usage in Ruby by eropple on Wednesday January 24, 2018 @12:18PM (#16224351)
"You have to specify your fields at compile time" is exactly not like ActiveRecord, though. That's exactly the downside to it for doing ActiveRecord.

At that point, you may (should) just use an object mapper like ROM that has no model-class hooks at all. (Which I actually like. But the Rails world disagrees.)

Re:Reducing Memory Usage in Ruby by sam0x17 on Monday January 29, 2018 @04:10PM (#16260642)
You effectively do specify your fields before runtime in Rails, though, in the form of your migrations. Furthermore all of the associations (belongs_to, etc), are arguably also specified before runtime since those helpers run when the class is parsed.
Re:Reducing Memory Usage in Ruby by eropple on Friday February 09, 2018 @02:20AM (#16338521)
I frequently don't use Ruby-based migrations. I use datastores defined elsewhere and use Sequel as a convenient and easy interface for tasks against those datastores.

You're trying to slice too fine. Crystal can't do this. That's OK. Own it, don't try to hide it.

Re:Reducing Memory Usage in Ruby by yxhuvud on Wednesday January 24, 2018 @11:23AM (#16223915)
If the problem is stated as you want a dynamic class to create instances where you call dynamic methods, then yes you need Ruby.

However, that is basically a lookup table using string based dispatch to decide what to execute, and such a lookup table can be done in anything.

Re:Reducing Memory Usage in Ruby by sam0x17 on Monday January 29, 2018 @04:12PM (#16260655)
Crystal "does" have dynamic methods if you are willing to override method_missing.
Re:Reducing Memory Usage in Ruby by yxhuvud on Tuesday January 30, 2018 @09:11AM (#16265376)
No, the dispatch is still decided during compilation.