It seems odd you can't do assignment into block vars, like:
[['a', 'b'], ['c', 'd']].each { |first, second| ... }
It's also almost odd that "casting' ints is done with
0.to_int64
versus
0.as(Int64)
(when the latter is used for everything else).
It would be nice if crystal had a "-E" option (like gcc) to show what all the macros expand to (the "full output" as it were).
It would be nice to have some tracer option that output each method call with its arguments so you could see what's going on.
It seems a bit odd to have the nil class named Nil
also it's frustrating you can't do this:
" abc" + obj
so then you try
"abc" + obj.to_s
and that fails *too* by default
but this works:
"abc#{obj}"
That's it for me, awesome language (possibly best I've run into so far), and I plan on continuing to use it.
It's also almost odd that "casting' ints is done with
0.to_int64
versus0.as(Int64)
(when the latter is used for everything else).You have some missconceptions here:
- The as pseudo-method doesn't transform an object; is just a promise you give to the compiler with something it can't know, but you, as human, can.
- Number literals, like `0`, are initialized as Int32, so if you "promise" a Int32 will be a Int64, as you do with `0.as(Int64)`, it will fail, because you are basically lying. If you know in advance you want a Int64 you can use the "typed number literals" like `0_i64`, `1324_u64`, etc.
- When you want a type to be re-instantiated as other type you need a `to_something` method, that will create a whole new object. So doing `0.to_i64` is the same of doing `Int64.new(0)` it is just a new object, there is no cast involved.
That would actually be quite doable I think. Might be worth experimenting with and discussing.
--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang...@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/5d83fa06-6d06-4c84-b6d0-8e79d33d87e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I think we need to
clear up some terminology here. Confusingly, #as
is a “cast” operator, as
well is Int32#to_i64
.
I was thinking about the possibility that #to_s
and #to_i32
could be replaced with #to(String)
and #to(Int32)
,
not modifying #as
at all.
--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang...@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/d7853c2f-5be5-4b49-91d4-b368d8acf6a3%40googlegroups.com.
I think we need to clear up some terminology here. Confusingly,
#as
is a “cast” operator, as well isInt32#to_i64
. I was thinking about the possibility that#to_s
and#to_i32
could be replaced with#to(String)
and#to(Int32)
, not modifying#as
at all.
On 20/10/16 22:00, Luis Lavena wrote:
Hello,--
On Thursday, October 20, 2016 at 5:14:06 PM UTC-3, TR NS wrote:
On Thursday, October 20, 2016 at 10:42:10 AM UTC-4, carlosgajardo wrote:
It's also almost odd that "casting' ints is done with
0.to_int64
versus0.as(Int64)
(when the latter is used for everything else).
You have some missconceptions here:
- The as pseudo-method doesn't transform an object; is just a promise you give to the compiler with something it can't know, but you, as human, can.
- Number literals, like `0`, are initialized as Int32, so if you "promise" a Int32 will be a Int64, as you do with `0.as(Int64)`, it will fail, because you are basically lying. If you know in advance you want a Int64 you can use the "typed number literals" like `0_i64`, `1324_u64`, etc.
- When you want a type to be re-instantiated as other type you need a `to_something` method, that will create a whole new object. So doing `0.to_i64` is the same of doing `Int64.new(0)` it is just a new object, there is no cast involved.
Still it would be nice if casting were `x.to(Int64)`
Not native english speaker here, but for me "to" implies some sort of conversion/transformation while "as" implies treatment of that instance (aka: casting)
"0".to_u64 # Converts "0" to 64bits unsigned integer
dog.as(Animal) # Treat Dog instance as it were an Animal instance (which will not allow me to call #bark method, given me a compiler error)
--
Luis Lavena
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang+unsubscribe@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/d7853c2f-5be5-4b49-91d4-b368d8acf6a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang+unsubscribe@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/d910d15c-e9b3-262d-9453-c18b2a4707a6%40rx14.co.uk.
The word you’re looking for is writability.
I think that
specifying the full type to convert to makes a lot of sense. I
don’t find myself using to_foo
often enough to be worried about a small amount of extra typing
such as that, and I think the readability gains are wroth it.
I’ve said before that I’m not a fan of the “more typing”
complaint in general because I spend much more time thinking
about and reading code than writing it.
It would be nice to try it out on a large project to see how it feels.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang...@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/CAGuHJrOVFdXCwLEbt1Q0Amp_cp6NNERF4FM7UtUs877Xw%3D_DRA%40mail.gmail.com.
I think we need to clear up some terminology here. Confusingly,
#as
is a “cast” operator, as well isInt32#to_i64
. I was thinking about the possibility that#to_s
and#to_i32
could be replaced with#to(String)
and#to(Int32)
, not modifying#as
at all.
class Animal def species "undefined" end end class Dog < Animal def species "Canis lupus" end end puts Dog.new.as(Animal).species
def to(t : Object.class) t.new self end
As for to(Foo), even it is pretty, I don't trust that people won't implement a generic transformationdef to(t : Object.class) t.new self endwhich is worse than awful
To add a bit more about this, it might be a good interface also with mappings that instantiate objects:cat = Animal.from(JSON, %({"type":"cat"})) # vs. Animal.from_json(...)
But it might get *waaaay* too verbose when dealing with nested classes like DB (DB::ResultSet):cats = Cat.from(DB::ResultSet, rs) # vs. Cat.from_rs(rs)
Does the compiler even allow that? This fails anyway
class Object def to(t) t.new self end end arr = [65_u8, 66_u8, 67_u8] puts arr.to_s #=> [65, 66, 67] puts arr.to_unsafe.to_s #=> Pointer(UInt8)@0x2592fd0 puts arr.to_unsafe.to(String) #=> ABC
But I'm not sure where it's getting the to_i32 from some magic conversion rule or something here?
# Returns an `Int32` by invoking `to_i32` on *value*. def self.new(value) value.to_i32 end
Does the compiler even allow that?
This fails anyway:
class A
def to(t : Object.class)
t.new self
end
end
puts A.new.to(Int32)
Roger,Does the compiler even allow that? This fails anywaythat's the problem, it does not always fails, and the result will be ambiguous. Look at thisclass Object def to(t) t.new self end end arr = [65_u8, 66_u8, 67_u8] puts arr.to_s #=> [65, 66, 67] puts arr.to_unsafe.to_s #=> Pointer(UInt8)@0x2592fd0 puts arr.to_unsafe.to(String) #=> ABC
But I'm not sure where it's getting the to_i32 from some magic conversion rule or something here?It is the definition of Int32.new itself, no magic
https://github.com/crystal-lang/crystal/blob/7f82f79bd1e3a7a1a73607e35c0662906736f1f8/src/int.cr#L607# Returns an `Int32` by invoking `to_i32` on *value*. def self.new(value) value.to_i32 end
The "to_i32" still feels a bit magic here.I agree we should still have to_i and to_f for ruby compatibility though
It would be nice if crystal had a "-E" option (like gcc) to show what all the macros expand to (the "full output" as it were).
Yes, +1
Meanwhile, for -E, you can use `{% debug() %}` inside you macros and it will print the expanded macro to the console.