我为什么要从 Python 转向 Crystal 语言 已翻译 100%

oschina 投递于 2018/06/12 16:01 (共 14 段, 翻译完成于 06-15)
阅读 9742
收藏 30
2
加载中

I have been a heavy user and lover of Python since 2011. At that time, when a good friend suggested I ditch Perl (eeek) and try Python instead, an entirely new world opened up to me. A world where readability counted above everything else and an explicit style ruled.

After around 7 years of using Python, I’m just as passionate about it now as I was then. However, as time goes on, one looks for new adventures and challenges. The time has come for me to try another language!

已有 2 人翻译此段
我来翻译

Python Challenges

Let me start by noting some of the challenges I’ve faced with Python:

  • Packaging: This is one area where most interpreted languages share challenges. Tools such as FPM make it really easy to ship an installable artifact that includes an entire virtualenv, but this still lacks the elegance of a single binary.

  • Static Typing: As someone who started with C++ and absolutely adored it, I do miss the type safety that I was used to from C++. This goes hand in hand with compile-time checks that really helped me ensure my code was of reasonable quality before even being executed.

  • Speed: Once again, a challenge shared by most interpreted languages. Python is fast enough for many tasks, but falls far behind compiled languages.

  • Verbosity: We only got f-strings in Python 3.6, which was truly a big relief. However, we still have extremely verbose self syntax in classes and constructors are littered with self.var = var, which may be partly addressed with data classes in Python 3.7.

  • Implicit Private Class Members: When I say private, I mean private damn it! As a former C++ guy, I found Python’s underscore-prefix style for private attributes and methods a little … hacky? :‘)

已有 1 人翻译此段
我来翻译

Further to this, I’m not sure that I really love the direction that Python is taking in a few areas, particularly around async and typing.

  • Coroutines: Although highly welcome, the new async functionality in Python feels very user-hostile and difficult to grasp. Existing code requires a good amount of work before it is made non-blocking too. I think this situation will improve in time as more libraries become available and as I understand and use the new libraries more though.

  • Type Annotations (and mypy): Honestly, type annotations are welcome … if they actually did anything in CPython. The new idea of using type annotations as part of various constructs (e.g. data classes) seems pointless without mainstream support in the main CPython distribution. In the meantime, mypy is not mainstream just yet but shows great promise as a type validator for Python in the future, particularly with the --strict flag enabled.

I should note that I’m still a massive fan and advocate of Python and think it’s still one of the best interpreted languages available today; particularly when you take into account its wonderful ecosystem and maturity.

已有 1 人翻译此段
我来翻译

What I’m Looking For

My starting point is really Python and Ruby. I’ve used Ruby from time to time where it was needed and really love it too. Ruby solves several problems that Python has (proper private/protected attributes, less verbose syntax .etc) but still suffers from performance problems and lacks static typing.

As such, I started looking for a new language with the following features:

  • Similar syntax to Python and Ruby

  • Single-binary distribution

  • Compiled, statically typed and fast

  • Object-oriented (oh classes, how I love you…)

已有 1 人翻译此段
我来翻译

Candidates

The following languages were ruled out:

  • Go: No keyword arguments, no exceptions, no classes, no generics and awful naming styles all led to me saying no to Go (although perhaps this simplicity is what attracts many to it). I have actually spent quite some time learning and coding in Go and found it frustrating at best. A language like C++ had made many advancements after C and offered us far greater flexibility yet it feels as though Go is taking us back to the days of C.

  • Elixir: A fascinating functional language, but lack of OO features and the fact that a single binary distribution is not the target of this language is a bit of a bummer for my use case. However, various folks in my team use Elixir as their primary language for all new projects and have found it great in use. Elixir has a rich and proven legacy and should definitely be considered if a functional language is what you’re after.

  • Rust: This is an interesting language that I have spent some time attempting to learn. Really, I just feel that Rust is not aimed at my use case; it is a rather complex language that just doesn’t seem to click with me and many others too.

  • Julia: This language is really targeted more for scientific computing as opposed my use case. It also lacks the OO abilities that I’m after.

  • Pony: A very fascinating language that seems to borrow a lot from Python, however it also borrows some things I dislike (e.g. underscore prefixed variables, lack of symmetry .etc). I generally didn’t feel that Pony aligned with the way I think nor did it have the same traction as other languages making it rather primitive currently.

已有 1 人翻译此段
我来翻译

Some languages that I’m really interested in and hope to examine further in the future are:

  • Nim: Nim was originally the front-runner as my next language and one which I hope to spend more time on in the future.

  • Swift: Another popular object-oriented language that definitely deserves attention beyond development of iOS and Mac apps.

But ultimately, I decided to commit to learning Crystal!

The reasons are as follows:

  • Crystal feels immediately familiar as it mostly follows Ruby’s syntax

  • It compiles into a fast, single executable

  • The entire standard library is written in Crystal which makes it very easy to read when required

  • It offers a full object-oriented approach similar to Ruby (which includes real protected and private members)

  • Crystal uses static typing but also provides unions (ability to define a variable that can be of multiple types)

  • It offers the ability to develop DSLs similar to Ruby (which is something I’ve always been interested in)

  • Bindings to C libraries are fully native and written in Crystal (similar to ctypes in Python, only better)

已有 1 人翻译此段
我来翻译

Caveats

Crystal is a very young language that still hasn’t hit 1.0. It often introduces breaking changes in releases and has limited libraries.

However, I plan to only use this language in my personal projects and was willing to become an early adopter due to the fact I feel that the language has enough promise to be worth using.

已有 1 人翻译此段
我来翻译

Experiences

Standard Library

The entire standard library is extremely easy to read and is something I reference all the time. The library also seems moderately extensive and is a great base to work with.

Here’s an example of the addition of arrays:


And here’s the function that obtains the extension of a file:


If you choose to try out Crystal, ensure you keep its source right by your side; it’s incredibly valuable and useful.

已有 1 人翻译此段
我来翻译

Binding to C Libraries

It’s amazing how easy this is!

Here’s an example of a binding to various functions that obtain user information from a Unix system:


Exception Handling

Similar exception handling is provided to both Ruby and Python:


Writing your own exceptions is trivial; simply inherit from the Exception class.

已有 1 人翻译此段
我来翻译

Import System & Namespaces

This was a bit of an adjustment coming from Python, but really brought me back to C++ days as Ruby follows a similar method to C++.

C++ namespaces are equivalent to Ruby/Crystal modules which you can define yourself. Requiring any library will import all items that it defines, so it’s always ideal to ensure that your entire library is contained within a module to avoid namespace pollution.

Initially I was a bit concerned about this, but I find it liberating being able to easily build up a module from any number of files. However, I will admit that it makes finding where things came from more of a challenge.


已有 1 人翻译此段
我来翻译
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(29)

青衫柏箴
青衫柏箴
很快你又多了一门放弃的语言
李强宇
李强宇
dlang+php,我的最爱!
osc_bd34fa5
osc_bd34fa5
没有大括号和return看着真心不舒服,而且最后的比较没意义,要比就都比第一个版本。还是热爱我php
不得瑟掉毛
不得瑟掉毛

引用来自“yak”的评论

我想知道学会了Crystal以后,找工作一月能挣多少钱?
跟这个没关系啊,在乎他能解决什么问题,比如源码保护,性能,开发难度
不得瑟掉毛
不得瑟掉毛
crystal一直在关注,只要他有了多线程,真就牛逼了
LinkerLin
LinkerLin
折腾,还不如用PHP
此用户已被系统屏蔽
此用户已被系统屏蔽
做等你换回来
gugu&gigi
gugu&gigi
哎,随着理论的完善和实践的丰富,学过编译原理的搞个程序语言不难,就像玩过电脑的都能搞个linux发行版出来。看到这种广告文就如同天天看到又有某某linux发行版之类的新闻一样,只觉得”他们还真爱折腾“。
a358003542
a358003542
要转那也是从一门动态语言转成动态语言,转成静态语言是哪个转法?
青春不再
青春不再
为什么要无视PHP
返回顶部
顶部