HumanG33k has quit [Remote host closed the connection]
HumanG33k has joined #crystal-lang
<FromGitter>
<3n-k1> is `y` garbage collected here? `x = 5; y = pointerof(x)`
<FromGitter>
<3n-k1> i would assume so, but i've dealt with weird languages lol
<FromGitter>
<asterite> it's on the stack, it disappears after you exit that method
<FromGitter>
<asterite> @mtsmmp_gitlab that's a yaml file with two documents, you should parse that first
<FromGitter>
<3n-k1> you're absolutely correct, i haven't touched c in a while haha. is there any case in crystal where you could have a pointer to something that isn't garbage collected?
<FromGitter>
<mtsmmp_gitlab> @asterite yeah bro front_matter.cr did the trick
<FromGitter>
<mtsmmp_gitlab> it does exactly that
<FromGitter>
<mtsmmp_gitlab> good thing that crystal is more mature
<FromGitter>
<mtsmmp_gitlab> i mean it wouldnt be hard to code that, but have a lib that does it already saves time
<FromGitter>
<mtsmmp_gitlab> this front matter module is like 100 lines of code
<FromGitter>
<mtsmmp_gitlab> could be even less
<FromGitter>
<Blacksmoke16> good to hear
duane has quit [Ping timeout: 265 seconds]
duane has joined #crystal-lang
postmodern has joined #crystal-lang
<postmodern>
what's the preferred way to expose macros in a library, so that all of the classes in the namespace have access to them? Define a module and include it everywhere? Explicitly call them like Macros.foo_macro?
<FromGitter>
<Blacksmoke16> that would be the most clear id say
<FromGitter>
<Blacksmoke16> sorry, the latter option
<FromGitter>
<Blacksmoke16> but pretty sure it would work w/o the FQN
<postmodern>
how do i define my own `delegate` macro? wondering how `delegate` defines it's `to:` keyword param
<FromGitter>
<Blacksmoke16> basically just defines a method for you, not much magic going on
alexherbo20 has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 240 seconds]
alexherbo20 is now known as alexherbo2
alexherbo20 has joined #crystal-lang
alexherbo2 has quit [Ping timeout: 256 seconds]
alexherbo20 is now known as alexherbo2
<oprypin>
@3n-k1: there was a conversation on this a few days ago. i concluded that the `pointerof` (the construct itself) is never safe. other ways of getting a pointer might be.
<postmodern>
trying to define my own delegate method with `to object` but getting this error "Error: can't use instance variables at the top level" with `struct_field capability, to: @struct.parm.capture`
<FromGitter>
<Blacksmoke16> got a playground example?
<FromGitter>
<Blacksmoke16> sounds like you're not using it within a class/struct
alexherbo2 has quit [Ping timeout: 256 seconds]
alexherbo2 has joined #crystal-lang
<postmodern>
it's within a class. will need to extract the example code into something easier to test.
<FromGitter>
<dscottboggs> is there a construct a reference to a local variable? for example ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ or pass a `Pointer` or `Slice` (of size 1) to the other object, but I feel like using a raw pointer is a smell unless there's a really good reason for it (since the length is technically unknown) and a slice isn't really for this [https://gitter.im/crystal-lang/crystal?at=5f482bf536e6f709fd084e0c]
<FromGitter>
<j8r> If `Bar` is a class, it could be passed to `Foo.new`
<FromGitter>
<dscottboggs> ahhh, that could do it
<FromGitter>
<franciscoadasme> hey everyone, I've been trying to find a way to implement something like copy-on-write for an array wrapper (via `method_missing` macro), that is, make a copy of the internal array before calling a mutating method on it. The use case is to expose an array of internal data that should not be modified by external entities, but I'd like to avoid doing copies of it every time that it is accessed. However, I
<FromGitter>
... cannot find a way to determine if a method is mutating the array or not :/... can anyone think of a way to do this?
<FromGitter>
<dscottboggs> no, this is a bit of a gripe I have with the type system personally
<FromGitter>
<dscottboggs> Only way I can think of is to use an immutable array (there's a shard for that) or manually make a list of all the methods which don't mutate an array and make a list to allow them, then make a copy on any missing method from that list
<FromGitter>
<Blacksmoke16> cant just call `.dup` or something?
<FromGitter>
<dscottboggs> also that seems more useful for something passed by copy instead of passed by reference like an array
<FromGitter>
<dscottboggs> would be cool to add mutation methods to `StaticArray` in a COW way
<FromGitter>
<franciscoadasme> @dscottboggs I thought of that but non-mutating methods added to Array would also trigger copies... so it is not ideal but it's the "best" idea so far
<FromGitter>
<j8r> If you pass a StaticArray, being a struct, it will be passed by copy
<FromGitter>
<j8r> This will be cheaper than `Array#dup`
<FromGitter>
<franciscoadasme> @Blacksmoke16 I'd like to avoid doing copies every time the array is accessed, especially if you're then going to generate a new array (e.g., calling map)
<FromGitter>
<Blacksmoke16> fair enough
<FromGitter>
<franciscoadasme> @j8r But you need to know the array size at compilation time, right?
<FromGitter>
<dscottboggs> no, i think you misunderstood.... if you manually write down a list of all the methods that *don't* mutate, you can check for that. ⏎ ⏎ I still think this would be more useful if you added to or wrapped StaticArray rather than wrapping Array
<FromGitter>
<dscottboggs> yes, for a StaticArray
<FromGitter>
<j8r> @franciscoadasme right, you don't in your case?
<FromGitter>
<dscottboggs> but that's the only way you can pass by copy, so there's no way to *not* do that and still pass by copy.
<FromGitter>
<dscottboggs> and you could still wrap a StaticArray in a reference (by putting it in some wrapper class like @j8r said) once you finish adding to the immutable base
<FromGitter>
<dscottboggs> actually I take back that second paragraph, that doesn't make sense
<FromGitter>
<j8r> however, if you only need an array for iterating, creating arrays can be avoided. ⏎ For example, a hash could store the difference between the initial and the virtual array
<FromGitter>
<j8r> For example, index 2 is modified, to the hash will be `{2 => ... }`
<FromGitter>
<j8r> Maybe there is a way to have "views" of the parts of the array by messing around with Slice. Then, we dig with low-level, unsafe code.
<FromGitter>
<franciscoadasme> sure... the idea is to have "mutable views" that make a copy of the original array before mutation
<postmodern>
is there something special about the `to` keyword in the definition of the `delegate` macro?
<FromGitter>
<Blacksmoke16> Got that example yet?
<postmodern>
Blacksmoke16, now my simplified example gives me a completely different macro error
<postmodern>
wait no, hmm
<FromGitter>
<dscottboggs> postmodern, no. The if you put an argument after a splat, it must be present as a named argument, and all args before it are folded into the splat