Giter Site home page Giter Site logo

Comments (10)

zw963 avatar zw963 commented on June 1, 2024 1

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. 😄, 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

from robe.

dgutov avatar dgutov commented on June 1, 2024

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).

from robe.

nanobowers avatar nanobowers commented on June 1, 2024

@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.

from robe.

dgutov avatar dgutov commented on June 1, 2024

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).

from robe.

dgutov avatar dgutov commented on June 1, 2024

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

from robe.

zw963 avatar zw963 commented on June 1, 2024

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.

from robe.

dgutov avatar dgutov commented on June 1, 2024

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.

from robe.

dgutov avatar dgutov commented on June 1, 2024

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

from robe.

zw963 avatar zw963 commented on June 1, 2024

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.

from robe.

dgutov avatar dgutov commented on June 1, 2024

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.

from robe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.