Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 08:23
    caspiano edited #10531
  • Dec 08 23:04
    HertzDevil edited #12623
  • Dec 08 22:57
    Blacksmoke16 edited #9745
  • Dec 08 22:57
    Blacksmoke16 labeled #9745
  • Dec 08 22:57
    Blacksmoke16 labeled #9745
  • Dec 08 22:54
    HertzDevil labeled #12828
  • Dec 08 22:54
    HertzDevil labeled #12828
  • Dec 08 22:54
    HertzDevil opened #12828
  • Dec 08 22:34
    straight-shoota edited #12827
  • Dec 08 22:32
    straight-shoota edited #12827
  • Dec 08 22:29
    straight-shoota labeled #12827
  • Dec 08 22:29
    straight-shoota labeled #12827
  • Dec 08 22:29
    straight-shoota opened #12827
  • Dec 08 22:15
    straight-shoota closed #12814
  • Dec 08 17:12
    straight-shoota assigned #12814
  • Dec 08 17:12
    straight-shoota assigned #12813
  • Dec 08 17:12
    straight-shoota assigned #12809
  • Dec 08 17:12
    straight-shoota assigned #12802
  • Dec 08 17:12
    straight-shoota assigned #12797
  • Dec 08 17:12
    straight-shoota assigned #12664
George Dietrich
@Blacksmoke16
crystal spec spec/path/to/file
:thumbsup:
mfiano
@mjfiano:matrix.org
[m]
I've been trying to think how to structure my module hierarchy correctly for a couple hours. I'm not used to this way of programming apparently.
George Dietrich
@Blacksmoke16
oh?
mfiano
@mjfiano:matrix.org
[m]
I have a few different structs, each in their own file under src/myproject/, and in a MyProject module. In src/myproject.cr I basically just do require "./src/myproject/*. Now I would like to add a "common.cr" file with common functionality between all the structs, implementing mostly class methods, and also available to users of the library. I can't seem to figure out how to get that to work.
George Dietrich
@Blacksmoke16
is the logic common to all the structs or only some of them?
mfiano
@mjfiano:matrix.org
[m]
Well, one such common method would be that nearly_equal?(x, y, rel_tol, abs_tol) method I wrote yesterday. This would be used in the implementation of several of each of the structs' instance methods.
George Dietrich
@Blacksmoke16
put it in a parent abstract struct?
mfiano
@mjfiano:matrix.org
[m]
I could look into that. I also want some of these methods to be public facing.
If that will work
George Dietrich
@Blacksmoke16
a public method declared in a parent type is available in all its children
a module/mixin is mainly ideal for like extra code that could be useful for any type. E.g. like the stdlib's Comparable, or Enumerable modules
but if something is common to multiple things, just define that once and use inheritance
mfiano
@mjfiano:matrix.org
[m]
Right, I just have to look up abstract types in Crystal again. I'm used to all of their methods also being abstract.
mfiano
@mjfiano:matrix.org
[m]
Thanks. I'll re-read that again. Your suggestion above also really clicked for me.
George Dietrich
@Blacksmoke16
and im assuming you actually want structs. I.e. are they immutable?
one gotcha is you cant inherit a non abstract struct, which might require some redesign if you wanted to have a base struct that was also usable on its own
mfiano
@mjfiano:matrix.org
[m]
That is something I'm still deciding on. I'm not sure if I want to allocate new stack memory or just use heap-allocated objects/mutability yet.
I will have to see how it is to be used and benchmark some I suppose. For now, they are structs though. I expect the change to class to be easy later if need be.
George Dietrich
@Blacksmoke16
:thumbsup:
mfiano
@mjfiano:matrix.org
[m]
I'm having a ton of fun learning, albeit slow, thanks for your help :)
George Dietrich
@Blacksmoke16
np
George Dietrich
@Blacksmoke16

is there a better way to create read addition bytes into a larger Bytes slice other than doing like:

    bytes = Bytes.new 4
    io.pos -= 3
    io.read_fully bytes

where the original bytes slice was 3

mfiano
@mjfiano:matrix.org
[m]
Do the property/getter/setter macros inline the generated method defs?
George Dietrich
@Blacksmoke16
im not sure, would be up to LLVM i'd think
could you even inline that? like since its internal state
mjfiano
@mjfiano:matrix.org
[m]
I guess it depends how they are implemented. In Lisp, struct members are regular functions that access the internal state, and are inlined by default, but can be told not to be.
I was just wondering if that was the case here, and if the AlwaysInline annotation was implied, or a way to opt in/out
George Dietrich
@Blacksmoke16
@HertzDevil might know
manveru
@manveru:matrix.org
[m]
undefined macro method 'Path#instance_vars' confused me way longer than i wanna admit... it refers to Crystal::Macros::Path and not Path :P
George Dietrich
@Blacksmoke16
yea...all that stuff is macro level types
manveru
@manveru:matrix.org
[m]
i was using Path around that area, so got quite confused :)
George Dietrich
@Blacksmoke16
that would do it
manveru
@manveru:matrix.org
[m]
i'm still fighting with macro evaluation order i think
George Dietrich
@Blacksmoke16
oh/
?
manveru
@manveru:matrix.org
[m]
basically... i want some kind of inheritance i guess
      macro derive_from(owner, *names)
        \{% for name in names %}
          \{% var = owner.resolve.instance_vars.find{ |v| v == name } %}
          property \{{name}} : \{{var.type}}
        \{% end %}

        def initialize( \{{ names.map{ |n| "@#{n}".id }.join("").id }} )
        end
        \{% debug %}
      end
the problem is that at this point, instance_vars is empty once more
George Dietrich
@Blacksmoke16
has to be used in the context of a method
got some example code of what you ultimately want to do?
manveru
@manveru:matrix.org
[m]
yeah... gimme a minute
George Dietrich
@Blacksmoke16
:thumbsup:
manveru
@manveru:matrix.org
[m]
i basically want to write something like this:
struct Nomad::Config
  include SimpleConfig::Configuration

  options_from Common::Config, nomad_datacenters, nomad_base_url, nomad_ssl_ca, nomad_ssl_key, nomad_ssl_cert
end
and look up the types in Common::Config for each of the attributes, and generate a typesafe constructor for it
George Dietrich
@Blacksmoke16
yea idt you can do that
manveru
@manveru:matrix.org
[m]
i'm not sure how that'd work as a method
George Dietrich
@Blacksmoke16
wont be able to iterate the ivars within a method, and you of cant create a method while you're in a method