robe icon indicating copy to clipboard operation
robe copied to clipboard

[Help Request] Not robe issue, just need some professional help on crystal-mode.

Open zw963 opened this issue 3 years ago • 10 comments

Hi, @dgutov , this is not a robe issue, it just a help request, for the Crystal lang

I guess you heard about it, you know, fast as C, Slick as Ruby, it truth, but more more than only this.

Null Safety (as Dart, Swift, Kotlin) make code sound,

Union Type + with the type_inference, make write Crystal almost like write Ruby.

Multiple dispatch allows you to have different versions of a method with the same name,

Concurrent Fiber as golang's goroutine/channel,

Plus the most wonderful compile time macro feature(which possible stolen from lisp), which make many of feature in ruby (method missing, Module.included etc) is possible in Crystal.

I really enjoy with the development process of LDD (Log Driven Development), Yes, just follow the directions of compile-time logging, we can fix almost all error. no null-point anymore.

Anyway, this is a special and feature unique language which as s good Rubyist, little effort to start.

Only one problem, the community is still very small, especially, not too much emacs user, emacs-crystal-mode is a fork of ruby-mode, but with some bugs, as early as 2018, the last commit is half one years ago, the current maintainer seem like is busy, and i create several issue recent days, most of about indentation issue, e.g. https://github.com/crystal-lang-tools/emacs-crystal-mode/issues/43, it quite annoy, i ever try fix it myself, but with no luck.

So, i desire, if you have free time, if can fix some them, thank you very much for you work on ruby-mode as the emacs official ruby-mode maintainer and others OS projects.

zw963 avatar Jun 26 '22 16:06 zw963

I wouldn't say I have a lot of time, so maybe someday. Happy to answer questions, though.

There also seems to be a fair amount of duplication, so I wonder how many differences there are between the languages. Is any Ruby program a valid Crystal program? Perhaps it might be more efficient to maintain just one language mode instead of two.

So far I see: 1) the abstract keyword, 2) the {% syntax (a macros thingy, apparently).

dgutov avatar Jul 04 '22 00:07 dgutov

@dgutov Unfortunately any arbitrary Ruby program is not a valid Crystal program. It is possible to create very simple (trivial) Ruby programs that can compile as valid Crystal, but this is more the exception than the rule.

nanobowers avatar Jul 04 '22 02:07 nanobowers

Is any Ruby program a valid Crystal program?

No, same as ruby is not the goal, it just follow the good part of ruby's design.

macro (it looks like elisp's macro i guess), annotate are invalid in ruby-mode.

And the colon serve both as type declare and return value declare.

# This methods greets *recipient*.
def say_hello(recipient : String)
  puts "Hello #{recipient}!"
end

# This method greets *times* times.
def say_hello(times : Int32)
  puts "Hello " * times
end

say_hello "World"
say_hello 3

# return type is String
def some_method : String
  "hello"
end

Tough, almost all case the type_inference always works, you don't need specify it. but, those all valid syntax.

And, it support untagged union type, like this:

if 1 + 2 == 3
  a = 1
else
  a = "hello"
end

# the type of a at compile time is a union type.
typeof(a) # : Int32 | String

# can be used as type declare directly
b : typeof(a)  # same as `b : Int32 | String`

b = 100
b = "hello world"

b = :hello  # Error: type must be (Int32 | String), not (Int32 | String | Symbol

this probably is all the supported keywords in Crystal.

this is a good chance to try this awesome language. :smile:, i am a newcomer use this language 2 months, tough, familiar with ruby make this learning curve more smoothness then ever, if you have any question about it, i will always try to answer it try my best.

Thank you very much, @dgutov

zw963 avatar Jul 04 '22 04:07 zw963

Okay. I should probably turn the question a little differently.

Does ruby-mode generally work okay for Crystal now? Or are there a lot of examples where it does the wrong thing?

It's about whether there should be two different major modes, or just one with minor tweaks (call it dialects, say).

dgutov avatar Jul 04 '22 10:07 dgutov

Macros do look like a pain to support, but it's not having separate crystal-mode makes it easier.

dgutov avatar Jul 04 '22 10:07 dgutov

Hi, @dgutov , i create a new repo for record all known different syntax part of Ruby and Crystal here, please have a look, and give me feedback.

All code in ruby_invalid.cr is totally valid crystal source code, no any error when run with crystal run src/ruby_invalid.cr, but each section(separate by -------------), have one or more syntax error when run it in ruby interpreter.

zw963 avatar Jul 04 '22 21:07 zw963

Cool. It seems like most of the syntax works okay-ish already.

What's missing is the enum ... end closer pair (maybe lib ... end as well) and tokenization/indentation support for private and abstract. private looks like it should be treated as a separate keyword (with support in the grammar and indendation logic), whereas abstract could be that too, but also seems like it should be ignored when before def (so, some support in the tokenizer).

I suggest you try implementing some of that yourself now, and we could get around to adding/merging this in some form to ruby-mode later.

dgutov avatar Jul 10 '22 01:07 dgutov

Or, and macros need support too, but the implementation could be trickier than the changes mentioned above.

dgutov avatar Jul 10 '22 01:07 dgutov

Cool. It seems like most of the syntax works okay-ish already.

Yes, though, it need following the newest changes from emacs official ruby-mode anyway.'

I suggest you try implementing some of that yourself now, and we could get around to adding/merging this in some form to ruby-mode later.

Okay, i will try, though i consider add a new major-mode crystal-mode to emacs which use ruby-mode as base, and only includes the changes part is better than merging somethings into ruby-mode.

zw963 avatar Jul 10 '22 09:07 zw963

though i consider add a new major-mode crystal-mode to emacs which use ruby-mode as base, and only includes the changes part is better than merging somethings into ruby-mode

If it can be done, that would be preferable, of course.

dgutov avatar Jul 10 '22 09:07 dgutov