[PYTHON] Overview of 5 "colon" languages (ː ☆ 1)

Introduction

Many widely used languages, such as Java / C ++ / C # / JS / PHP, are separated by a semicolon (;). On the other hand, there are many languages such as python / ruby / scala that are recommended to be separated by line breaks. There are quite a lot of scripting language lovers who hate semicolons, Some people feel that they lose when using a semicolon-separated language (I).

That aside There are quite a lot of languages that use a colon (ː) as a delimiter for instructions.

In major places, python, objective-c, etc. Smalltalk was created in the 1970s. And the up-and-coming emerging language Nim and the smalltalk-like language spry on Nim.

I started using python at work, so I started using these 5 languages I happened to try it, and all of these I noticed that the colon (ː) plays an important role in many ways. For the time being, I would like to expose the memo writing, which is also called a colon language, as a refreshing agent for my brothers who are usually exhausted by the semicolon language SI.

Or if you write it like a story ː

――When you call it colon-type languages, it's kind of exaggerated like linguistics. ――Respect Yukorin, who has a similar ground ball (?), And call it the Alien language. ... Yuko Rin, in 2017, she will switch from a mamatale to a single mother route ... ――If you write "colon ☆ word", it will appear in some anime ...

Colon system ① python

A typical scripting language, everyone's python, makes heavy use of colon notation. Write if, for, and class definitions using colons.

Example:

person.py


class Person():
    def __init__(self, name, age):
        self.name, self.age = name, age
        self.address = None

Comparing with the fastest master series of basic grammar that was popular a long time ago, [Python Basic Grammar Fastest Master](http://d.hatena.ne.jp/dplusplus/20100126/p1 The frequency of using colons in) is more than 10 times that of Ruby Basic Grammar Fastest Master. The style of using tab delimiters and colons used by Python is also used in some other languages (Boo on .NET, Nim compiled into C languages, etc.). I think Smalltalk, the original colon language, is the only one in the fastest master series of basic grammar that uses more colons than python.

Colon system ② objective-c

Objective-c has become too famous as an iOS development language. Colon notation is often used in object-oriented parts influenced by smalltalk. For example, the method setName that takes name and price as arguments is defined as follows.

method.m


- (void)setName:(NSString *)name price:(NSNumber *)price {
   _name = name;
   _price = price;
}

--Source Objective-C 2.0 Basic Grammar Fastest Master #The fastest master uploaded 2015 years ago. If you don't use Objective-C 2.0, it may be recommended for some people. I will put it to practical use.

For why the method notation is not setName (name, price) even though it is a C language, it is better to look at the original smalltalk.

--Objective-C is fully compatible with C language, and C functions are written in C language.

Objective-C, I'm thinking of putting it to practical use, so I'll write one article in the near future:

--Not limited to Objective-C and colon, the symbol Osugi. .. ..

Colon system ③ Smalltalk

For the historical significance of Smalltalk, see Smalltalk developer Alan Kay's special class special number (NHK). I think that.

By the way, if you continue to refer to Fastest Master, you can see the Smalltalk notation that uses more colons than python.

Let me quote some:

Use colons to define arrays and access arrays (selector description).

array.st


array := #(a b c). "Array definition"
array at: 2.          "Extracting the second element of the array=> #b "

Definition of dictionary and addition to dictionary

dict.st


dict := Dictionary new.
dict at: #a put: 1. "Add to dictionary"
dict at: #b put: 2. "”"
dict at: #c put: 3. "”"
dict   "=> a Dictionary(#a->1 #b->2 #c->3 ) "

Definition of anonymous functions using blocks

The so-called bracket notation is called a block, which allows you to define an anonymous function.

block.st


block1 := [:a :b | a + b]. "Assign an anonymous function that takes arguments a and b to the variable block1"
block1 value: 3 value: 4.           "Give block1 values 3 and 4=> 7 "

Objective-c has a similar notation, but I think Smalltalk is smarter. If you are interested, please read PPAP with Smalltalk-76 and Squeak / Pharo-Comparison of Smalltalk between generations beyond 40 years of space-time-. Above, you can touch the well-known Smalltalk execution environment (Squeak / Pharo, etc.). I got "Introduction to Squeak-Future programming environment from the past" for 1 yen on Amazon, which seems to be a good book for learning object-oriented programming, so I wonder if I will write something. sq

Colon system ④ Nim

Nim, a typed language that uses a Python-like indentation structure and is compiled into C, is a colon-heavy language that seems to be thin on Smalltalk.

Let me quote from a nice note Syntax of Nim:

for and if

for.nim


for b in 1..10:
  if b == 5:
    continue
  if b == 7:
    break
  echo b
# (1, 2, 3, 4,6 and one line are output)

Procedure (function) arguments and return values

proc.nim


proc isPositive(i: int): bool = i >= 0

Including procedure arguments and return values The type definition is a colon notation like java.

If you know that proc is something like JS function or go func, It will be easy to read.

Exception handling try-except-finally

try.nim


var f: File
if open(f, "sometext.txt"):
    try:
      ...  #Something processing
    except SomeError:  #If SomeError occurs in the try clause, here
      echo "some error"
    except AnotherError:  #If AnotherError occurs here
      echo "another error"
    except:  #If you get any other errors here
      echo "unknown erro"
      raise  #If only raise, throw the same exception up
    finally:
      echo "finally"  #Must be executed

The syntax is about the same as Python. Nim's colon notation is highly readable and fast to execute. It's the expected compilation language.

Colon system ⑤ Spry on Nim

Smalltalk-style scripting language Spry written in Nim. As it is a Smalltalk-like scripting language with a high affinity with the Nim language, which has many colons, Spry may be the number one candidate for a secret colon-heavy language.

――If you know a language full of colons, please let me know.

Below, the source is the Language Manual on the official website.

Function func

Function definition is done in Smalltalk taste. Use a colon in the argument

func1.sy


foo = func [:x + 4]
# Prints 9 on stdout
echo foo 5

When it comes to two arguments, it becomes a nicer colon notation.

func2.sy


add:to: = func [:x + :y]
echo (add: 5 to: 6)   # prints "11"

# And a method in the same way
add:and: = method [self + :x + :y]
echo (3 add: 5 and: 6) # prints "14"

Add: ~ to: and add: ~ and: are selector descriptions in Smalltalk. Like Smalltalk, the description is like natural language (English).

Condition block

When it comes to conditional branching, if is not adopted and it is written in colon. In other languages

if a >10 then 20 else 10

But I would write ...:

cond.sy


foo = func [:a > 10 then: [20] else: [10]]

echo foo 5  # prints 10
echo foo 12 # prints 20

Hmmm, this seems to be like or dislike. However, the description is very concise, and it will be fun if it is properly combined with Nim. More spectacular colon notation appears in the latter half of the manual.

Continue ... maybe.

As you can see from the fact that this is a fill-in-the-blank for Nim Advent Calendar 2016, I wrote it because I found something in common with the colon system while exploring the origin of the new language Nim + Spry that I am paying attention to. I couldn't write the details, but it's quite interesting to look at the colon notation of each language and the design concept of the language. As a bonus, I think that colon-heavy languages have something in common with the modest use of semicolons (;) and commas (,), which are often used in java (though they don't apply to objective-c).

After digging deeper into the colon notation of each language, I may write it again, so for the time being, I will add (ː ☆ 1) to the title as the first part of the series ː)

Recommended Posts

Overview of 5 "colon" languages (ː ☆ 1)
[Introduction to cx_Oracle] Overview of cx_Oracle
Features of programming languages [Memo]
Overview of Docker (for beginners)
The popularity of programming languages
First, an overview of odo (odoo 13)