HumanG33k has quit [Remote host closed the connection]
HumanG33k has joined #crystal-lang
<FromGitter>
<davidepaolotua_gitlab> Hi everyone, just to be sure I didn’t miss it anywhere… In Crystal, it’s currently not possible to have intersection types, am I right?
<FromGitter>
<naqvis> Hi, got an example?
<jhass>
you're thinking like java interfaces or rust traits?
<jhass>
Crystal solves this through automatic union types and duck typing
<jhass>
just don't type restrict your method argument and assume the methods are there on what you're passed
<FromGitter>
<davidepaolotua_gitlab> yup, but that would ensure that objects are either Displayable OR Moveable
<FromGitter>
<davidepaolotua_gitlab> if I want them to be Displayable AND Moveable, union types are not what I should look for, or am I missing anything?
<jhass>
Give it a try, turns out most of the time you don't care, just that you can trat them like that :)
<jhass>
*treat
<FromGitter>
<davidepaolotua_gitlab> Ok then :) I’ll give it a try
HumanG33k has quit [Quit: Leaving]
HumanG33k has joined #crystal-lang
Jenz has joined #crystal-lang
Jenz has quit [Quit: leaving]
woodruffw has quit [Ping timeout: 256 seconds]
<FromGitter>
<RespiteSage> @naqvis Interesting solution. The working code I have uses a ternary operator to map to the element or Nil, but I like the version that rejects `Nop` better.
<FromGitter>
<naqvis> `Nop` one looks like a magical one, as one need to know why its Nop but not Nil. I prefer to be explicit in my code as it makes easy to read and reason about.
<FromGitter>
<naqvis> also don't know the reason behind this inconsistency of `map` in normal code and macroland. normal code it should have returned `nil` instead of an empty node `Nop`
<FromGitter>
<Blacksmoke16> oo i think i might actually know how to fix this one
<FromGitter>
<Blacksmoke16> whats the issue? `.each_with_index` returns `Nop` instead of `Nil` type?
<FromGitter>
<RespiteSage> Yeah, that's fair. Right now the code is a PoC for myself, but if I ever use it for anything, I'll need to make it explicit. I'll leave my ternary for now.
<FromGitter>
<RespiteSage> @Blacksmoke16 Yeah, I think that's it. Lemme write a quick carc example.
<riffraff169>
hello, im trying to write a class....i need to initialize the class with an array of arrays (specifically: [ ["a","b"] , ["c","d"], ... ]
<riffraff169>
i keep getting either: Error: wrong number of type vars for Array(T) (given 2, expected 1)
<riffraff169>
or: Error: can't infer the type of instance variable '@tokens' of Parser
<FromGitter>
<Blacksmoke16> can you share the code?
<riffraff169>
def initialize(tokens : Array)
<riffraff169>
@tokens = tokens
<riffraff169>
@tp = 0
<riffraff169>
end
<FromGitter>
<Blacksmoke16> prob would be like `def initialize(@tokens : Array(Array(String))); end`
<riffraff169>
ah let me try that
<FromGitter>
<Blacksmoke16> need to be more specific than just `Array`
<riffraff169>
still wrong number of type vars for Array(T):
<riffraff169>
ive been looking through the docs, but all examples are single dimension (Int32, String, etc)....not arrays of mixed, so not quite getting my syntax right
<FromGitter>
<Blacksmoke16> so two things going on
<FromGitter>
<Blacksmoke16> first the type of tokens is actually `Array(Array(String | Int32))`, since the inner arrays can contain either strings or int32s
<riffraff169>
ok, i see that
<FromGitter>
<Blacksmoke16> next you have to help the compiler a bit by adding a `of Array(String | Int32)`
<FromGitter>
<Blacksmoke16> otherwise the inner arrays that do not contain int32s would not be allowed to contain them
<FromGitter>
<Blacksmoke16> so that basically says, each inner array can contain either strings or int32s, even if they don't currently
<riffraff169>
so it isnt inferring that @tokens should be same type as tokens? even though i say tokens = ... and @tokens = tokens?
<FromGitter>
<Blacksmoke16> thats the thing, they aren't the same w/o the `of Array(String | Int32)`
<riffraff169>
hmm
<FromGitter>
<Blacksmoke16> like, look at the error message: It says the type is `Array(Array(Int32 | String) | Array(String)) | Array(Array(Int32 | String))`
<riffraff169>
hmm, let me test something, thanks
<FromGitter>
<Blacksmoke16> main problem being the inner arrays that contain only strings, the type of the array would be like `Array(Array(String |Int32) | Array(String))`
<FromGitter>
<Blacksmoke16> meaning not all inner arrays are allowed to contain int32s
<riffraff169>
yeah, right now all inner arrays are [String,String] or [String,Int32] ([String,String|Int32]), and given my current requirements im not sure that will ever change
<riffraff169>
so where would the `of Array(String|Int32)` be....should i declare @tokens with that type before trying to assign tokens to it?
<riffraff169>
im assuming tokens would be copied to @tokens (value instead of reference), which is fine
<FromGitter>
<Blacksmoke16> its at the very end of line 1, if you scroll all the way to the right
<FromGitter>
<Blacksmoke16> arrays are passed by reference
<riffraff169>
ah, hmmm....
<riffraff169>
well, thats fine, at this point it is immutable anyway, so
<riffraff169>
well, in this small example i statically assigned tokens, but tokens is actually generated by another class, and that is the result....so i cant but `of...` at the end of tokens because it is already set somewhere else
<riffraff169>
my first class has this: tokens = [] of Array(String|Int32)
<riffraff169>
then that var has values added to it `tokens += [token]`
<riffraff169>
then tokens is passed to parser
<FromGitter>
<Blacksmoke16> prob should just use `<<`
<FromGitter>
<Blacksmoke16> but is that not working?
<riffraff169>
well gee, something somewhere, got it to working with this:
<FromGitter>
<Blacksmoke16> also you can add the `@` to the initializer arg, like `@tokens : `
<FromGitter>
<Blacksmoke16> so you dont have to do `@tokens = tokens`
deavmi has quit [Read error: Connection reset by peer]
deavmi has joined #crystal-lang
<riffraff169>
probably because i had String,String....i think i was thinking (for some reason) like a tuple, where i had to declare type of every index...but that wasnt making sense to me because an array is inherently variable...
<riffraff169>
thats working, thanks for the help
<riffraff169>
im from a c background, then ruby, so the cross between static and kind of dynamic is kind of weird
<FromGitter>
<Blacksmoke16> Tuples are also a thing in crystal land
<riffraff169>
im sure people have done it, but when i tried to load and access yaml it gave me errors because of not knowing the types (YAML::Any)...so basically yaml cant really be freeform, must have a schema
<FromGitter>
<Blacksmoke16> it can but you have to be a bit more explicit
<FromGitter>
<Blacksmoke16> i.e. instead of like `yaml_data["name"]` you have to do like `yaml_data["name"].as_s` to tell it it should be a string
<riffraff169>
yeah, i was mixing the two...can a tuple have a mixed type? (String,String|Int32), or must they all be the same
<riffraff169>
yeah, but that means you have to know ahead of time what type it is, which moves the data into the code, sort of
<riffraff169>
but im not working with yaml right now so im not investigating that further
<FromGitter>
<Blacksmoke16> if you're working with data it most likely has some predefined structure
<FromGitter>
<Blacksmoke16> so yes that means your code would know about the properties of that data, but it allows you to work with it in an easier/more type safe way
<riffraff169>
thanks for the help, i like crystal so far, i like the static binaries and no gc...well, not static binaries as such (dynamic doesnt bother me), but actually binaries rather than interpreted
<FromGitter>
<Blacksmoke16> however if the data is totally dynamic you ofc cant use that approach
<FromGitter>
<Blacksmoke16> crystal has a GC btw...
<FromGitter>
<Blacksmoke16> and you can get static binaries
<FromGitter>
<Blacksmoke16> make*
<riffraff169>
i wonder what crystals gc is, perf wise, compared to ruby, python, go, or java...mainly go or java....ive seen too many issues with java stop the world gcs
<riffraff169>
i was trying to do something with yaml some months ago, and the yaml i had was kind of freeform....basically a list of multiple types, so had to determine the type of that section before you could do anything with it...
<riffraff169>
yeah, thats kind of what i would have done...had code that checked if a field was present, and if so, then access relevant data through one method, if not, or had different fields, then do other things
<riffraff169>
basically unstructured data i guess, but thats not always great
<riffraff169>
thanks for the help, im going to continue with my program...
<FromGitter>
<Blacksmoke16> 👍
<riffraff169>
trying to write a compiler for 6502 assembly that can be run on c64....probably not necessary (there are others), but im learning crystal, and it is very nostalgic doing c64 programming again
<FromGitter>
<Blacksmoke16> :S sounds like a plan
maxpowa has quit [*.net *.split]
daemonwrangler has quit [*.net *.split]
maxpowa has joined #crystal-lang
daemonwrangler has joined #crystal-lang
<FromGitter>
<wyhaines> I was a freaking wizard with 6502 assembler. Those were the days!
alexherbo2 has quit [Ping timeout: 256 seconds]
<riffraff169>
yeah i programmed the c64 back in the 80s....so fun stuff...people are still making games and demos for it