Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 05:11
    naqvis labeled #13036
  • 05:11
    naqvis opened #13036
  • 00:26
    straight-shoota edited #13035
  • 00:18
    straight-shoota labeled #13035
  • 00:18
    straight-shoota assigned #13035
  • 00:18
    straight-shoota opened #13035
  • 00:10
    straight-shoota edited #13034
  • Jan 31 23:31
    HertzDevil labeled #13034
  • Jan 31 23:31
    HertzDevil labeled #13034
  • Jan 31 23:31
    HertzDevil opened #13034
  • Jan 31 22:07
    straight-shoota ready_for_review #12886
  • Jan 31 21:37
    straight-shoota edited #13010
  • Jan 31 21:02
    straight-shoota synchronize #13020
  • Jan 31 21:00
    straight-shoota labeled #13033
  • Jan 31 21:00
    straight-shoota labeled #13033
  • Jan 31 21:00
    straight-shoota labeled #13033
  • Jan 31 21:00
    straight-shoota opened #13033
  • Jan 31 20:53
    straight-shoota synchronize #13020
  • Jan 31 20:35
    straight-shoota closed #13011
  • Jan 31 18:15
    straight-shoota labeled #13032
manveru
@manveru:matrix.org
[m]
anw, i gotta get some sleep, thanks for the feedback :)
George Dietrich
@Blacksmoke16
o/
postmodern
@postmodern:matrix.org
[m]
@didactic-drunk: should we add Process.fork to crystal-posix? @kevinsjoberg was asking about lack of Process.fork. https://gist.github.com/postmodern/a98e68dff96163b5f234cd9de1c5c75d
2 replies
mfiano
@mjfiano:matrix.org
[m]
I'm trying to solve a visibility issue.
I have private class method self.foo at the module level in one file, and in another file I have a struct defined in the same module. I would like some of the instance methods of the struct to be able to use the private foo method at the module level, but I cannot fully qualify it, and it is not part of the struct so I'm not sure if this is possible. I'm trying to write a file of private helper methods that are used in several other files/structs
mfiano
@mjfiano:matrix.org
[m]
Cancel that. I seemed to have figured out the right incantation to extend the module with private methods according to crystal doc.
From IRC (bridge bot)
@FromIRC
<kevinsjoberg> @postmodern, great idea!
George Dietrich
@Blacksmoke16
@mjfiano:matrix.org could you just make it protected
postmodern
@postmodern:matrix.org
[m]
@didactic-drunk @kevinsjoberg hmm nevermind, looks like Process.fork does exist but is nodoc'ed / part of the private API. https://github.com/crystal-lang/crystal/blob/1.0.0/src/process.cr#L62-L98
mfiano
@mjfiano:matrix.org
[m]
@Blacksmoke16: Maybe, though I'm not sure what that means for module methods...the docs talk about instances. I found a way with private though.
George Dietrich
@Blacksmoke16
mfiano
@mjfiano:matrix.org
[m]
@Blacksmoke16: Interesting. I did something like this: https://play.crystal-lang.org/#/r/bkuh
mfiano
@mjfiano:matrix.org
[m]

Mine probably doesn't do exactly what I expect.

Also, stupid question: I've never seen the double colon syntax for module name as in your example...where is this documented?

George Dietrich
@Blacksmoke16
It's equivalent to creating two modules and nesting them
mfiano
@mjfiano:matrix.org
[m]
Is there a convention when naming methods that side-effect their instance? Some languages use a ! suffix.
Ali Naqvi
@naqvis
yes, Crystal follows same conventions. methods ending with bang represents inline mutation. stdlib have few of them
mfiano
@mjfiano:matrix.org
[m]
Thank you
Ali Naqvi
@naqvis
similarly methods returning bool are usually end with ?
George Dietrich
@Blacksmoke16
! Can also represent methods that raise, while ? Returns nil
But I see more of like .foo and.foo? Versus .foo! And.foo?
mfiano
@mjfiano:matrix.org
[m]
FYI: This convention is very old, derived from Scheme (a Lisp)
mfiano
@mjfiano:matrix.org
[m]
I think I decided on classes over structs. pass-by-value is sort of convoluting my API already.
re: yesterday's discussion
George Dietrich
@Blacksmoke16
:thumbsup: usually the suggested approach is just use classes to start
then can fix bottlenecks as needed, OR if the obj is for immutable and small
mfiano
@mjfiano:matrix.org
[m]
Yeah I chose structs first because this is ultimately going to be for high performance math routines used in math visualization animations
But that was a mistake...for now
George Dietrich
@Blacksmoke16
are you created a lot of objects in a loop or something?
that would prob be noticable diff
mfiano
@mjfiano:matrix.org
[m]
The math routines will typically have 2 different methods for each...one that allocates, and one that mutates...so, using classes should be fine, and I can always mutate a pre-allocated object before the loop
I mean I can always mutate inside the loop, a pre-existing object
My Lisp code I'd like to replace is over-optimized. I create temporary objects at init time and never allocate at runtime...heh
George Dietrich
@Blacksmoke16
:thumbsup: sounds like a plan
mfiano
@mjfiano:matrix.org
[m]
One thing that I still haven't gotten used to is blocks. It's very strange how methods are not first-class functions, coming from Lisp and a bit of Rust.
George Dietrich
@Blacksmoke16
Hm?
mfiano
@mjfiano:matrix.org
[m]
top-level variables being inaccessible in methods, intentionally restricting closures in favor of a separate construct (blocks).
I'm just not sure why that is the case compared to first-class functions. Maybe I don't fully understand blocks
George Dietrich
@Blacksmoke16
Have you read about procs?
mfiano
@mjfiano:matrix.org
[m]
Yeah, and I don't know how that differs from a lambda/anonymous function in other languages.
George Dietrich
@Blacksmoke16
It's the same concept
A method with a block is similar to a method that accepts an anonymous callback function
mfiano
@mjfiano:matrix.org
[m]
Yeah...it's just confusing me because lambda calculus is so simple and doesn't require any of that to do the same things.
George Dietrich
@Blacksmoke16
:shrug:
mfiano
@mjfiano:matrix.org
[m]
module Origin
  private DEFAULT_TOLERANCE = 1e-7

  def self.=~(x : Float64, y : Float64, rel_tol : Float64 = DEFAULT_TOLERANCE, abs_tol : Float64 = rel_tol)
    (x - y).abs <= Math.max(abs_tol, rel_tol * Math.max(x.abs, y.abs))
  end
end
Interestingly, this can be called as a fully-qualified method: Origin.=~(a, b, rel_tol: c, abs_tol: d), or a binary operator without qualification if the defaults are sufficient: a =~ b. I'm not sure why the former requires qualification.
I think mayhbe my code is wrong and it is calling the wrong overload
No it is calling the same method in both cases.
mfiano
@mjfiano:matrix.org
[m]
I think everything is fine actually. If anyone has any suggestions for improvements on this snippet, I'd appreciate it: https://play.crystal-lang.org/#/r/bky9
George Dietrich
@Blacksmoke16
you can just do property x : Float64, y : Float64
save you 2 lines :p
mfiano
@mjfiano:matrix.org
[m]
Oh thanks
I just found what I think is a very minor compiler bug