언어별 문법 정리
2021년 7월 24일 형식 및 언어 추가
2019년 8월 29일 초안 작성
Data Type
Number | String | Constants | |
---|---|---|---|
C++ | int a = 0; |
std::string a = "a"; |
const int a = 0; |
Java | int a = 0; |
String a = "a"; |
final int a = 0; |
Python | a: int = 0 |
a: str = 'a' |
N/A |
Go | var a int = 0 |
var a string = "a" |
const a int = 0 |
TypeScript | let a: number = 0 |
let a: string = 'a' |
const a: int = 0 |
Kotlin | var a: Int = 0 |
var a: String = "a" |
val a: Int = 0 |
Swift | var a: Int = 0 |
var a: String = "a" |
let a: Int = 0 |
- TypeSript has two variable declarations that
var
is function scoped andlet
is block scoped. - Swift has Optional, Kotlin has Nullable type. Both have same syntax.
var a: Int? = 0
List / Dynamic Array | Hash Table | |
---|---|---|
C++ | std::vector<int> a{1, 2, 3}; |
std::unordered_map<std::string, int> a{ {"a", 1}, {"b", 2} }; |
Java | List<Integer> a = new ArrayList<>(Arrays.asList(1, 2, 3)); |
Map<String, Integer> a = new HashMap<>(); |
Python | a: List = [1, 2, 3] |
a: Dict = {"a": 1, "b": 2} |
Go | var a []int = []int{1, 2, 3} |
var a = map[string]int{"a": 1, "b": 2} |
TypeScript | let a: Array<number> = new Array(1, 2, 3) |
let a: Map<string, number> = new Map([["a", 1], ["b", 2]]) |
Kotlin | var a: List<Int> = arrayListOf(1, 2, 3) |
var a: Map<String, Int> = mapOf("a" to 1, "b" to 2) |
Swift | var a: [Int] = [1, 2, 3] |
var a: [String: Int] = ["a": 1, "b": 2] |
- Go type can be omitted like
var a = []int{1, 2, 3}
. - TypeScript instantiation can be simplified like
let a: Array<number> = [1, 2, 3]
. - Kotlin, if you want to make
MutableList
you should rely on type inference likevar a = arrayListOf(1, 2, 3)
orvar a: MutableList<Int> = mutableListOf(1, 2, 3)
.- Remember, Kotlin is a functional language.
Loops
C++
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
Java
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
Python
# Case #1
sum: int = 0
for i in range(1, 10 + 1):
sum += i
# Case #2
total = sum(i for i in range(1, 10 + 1))
Go
var sum int = 0
for i := 1; i <= 10; i++ {
sum += i
}
TypeScript
let sum: number = 0;
for (let i = 1; i <= 10; i++) {
sum += i;
}
Kotlin
var sum: Int = 0
for (i in 1..10) {
sum += i
}
Swift
var sum: Int = 0
for i in 1 ... 10 {
sum += i
}
Iterate through Array
C++
std::string foo[] = {"A", "B", "C"};
for (std::string f : foo) {
std::cout << f << std::endl;
}
Java
String[] foo = new String[]{"A", "B", "C"};
for (String f : foo) {
System.out.println(f);
}
Python
from typing import List
foo: List = ['A', 'B', 'C']
for f in foo:
print(f)
Go
var foo = []string{"A", "B", "C"}
for _, f := range foo {
fmt.Println(f)
}
TypeScript
let foo: string[] = ['A', 'B', 'C'];
for (const f of foo) {
console.log(f);
}
Kotlin
var foo: Array<String> = arrayOf("A", "B", "C")
for (f: String in foo) {
println(f)
}
Swift
var foo: [String] = ["A", "B", "C"]
for f in foo {
print(f)
}
Conditions
…
Templates, Generics
C++
template<class T, class U>
bool are_equal(T a, U b) {
return (a == b);
}
are_equal(10, 10.0)
Java
public static<T, U> boolean are_equal(T a, U b) {
return a == b;
}
are_equal(10, 10.0);
Python
# Case #1
def are_equal(a, b):
return a == b
are_equal(10, 10.0)
# Case #2
from typing import TypeVar
T = TypeVar('T')
U = TypeVar('U')
def are_equal(a: T, b: U) -> bool:
return a == b
are_equal(10, 10.0)
Go
// Not Supported. But probably plan to support in Go 2 version.
// <https://blog.golang.org/why-generics>
TypeScript
function are_equal<T, U>(a: T, b: U): boolean {
return +a == +b;
}
are_equal<number, number>(10, 10.0);
Structs
C++
struct Product {
int weight;
double price;
};
Product apple;
apple.price = 10;
Java
class Product {
private int weight;
private double price;
public void setPrice(double price) {
this.price = price;
}
}
Product apple = new Product();
apple.setPrice(10);
Python
from dataclasses import dataclass
# Data Classes module is only work in Python 3.7 and above.
# A backport of the module for Python 3.6 <https://pypi.org/project/dataclasses/>
@dataclass
class Product:
weight: int = None
price: float = None
apple = Product()
apple.price = 10
Go
type Product struct {
weight int
price float64
}
var apple Product
apple.price = 10
TypeScript
interface Product {
weight: number,
price: number,
}
let apple = {} as Product;
apple.price = 10;
Classes
C++
class Rectangle {
int width, height;
public:
Rectangle(int, int);
int area();
};
Rectangle::Rectangle(int x, int y) {
width = x;
height = y;
}
int Rectangle::area() {
return width * height;
}
Rectangle rect(3, 4);
std::cout << rect.area() << std::endl;
Java
class Rectangle {
int width;
int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public int area() {
return this.width * this.height;
}
}
Rectangle rect = new Rectangle(3, 4);
System.out.println(rect.area());
Python
from dataclasses import dataclass
@dataclass
class Rectangle:
width: int
height: int
def area(self):
return self.width * self.height
rect = Rectangle(3, 4)
print(rect.area())
Go
type Geometry interface {
area() int
}
type Rectangle struct {
width int
height int
}
func (r *Rectangle) area() int {
return r.width * r.height
}
var rect Geometry = &Rectangle{3, 4}
fmt.Println(rect.area())
TypeScript
class Rectangle {
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
area(): number {
return this.width * this.height;
}
}
const rect = new Rectangle(3, 4);
console.log(rect.area());
Inheritance
C++
class Animal {
protected:
std::string name;
public:
Animal(std::string n) {
name = n;
}
void move(int);
};
void Animal::move(int distanceInMeters = 0) {
std::cout << name << " moved " << distanceInMeters << "m." << std::endl;
}
class Horse : Animal {
public:
Horse(std::string n) : Animal(n) {}
void move(int);
};
void Horse::move(int distanceInMeters = 45) {
std::cout << "Galloping..." << std::endl;
Animal::move(distanceInMeters);
}
Horse tom("Tommy");
tom.move(34);
Java
public class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public void move(int distanceInMeters) {
System.out.printf("%s moved %dm.%n", this.name, distanceInMeters);
}
}
public class Horse extends Animal {
public Horse(String name) {
super(name);
}
public void move(int distanceInMeters) {
System.out.println("Galloping...");
super.move(distanceInMeters);
}
}
Animal tom = new Horse("Tommy");
tom.move(34);
Python
from dataclasses import dataclass
@dataclass
class Animal:
name: str
def move(self, distanceInMeters: int = 0):
print('%s moved %sm.' % (self.name, distanceInMeters))
class Horse(Animal):
def move(self, distanceInMeters: int = 45):
print('Galloping...')
super().move(distanceInMeters)
tom: Animal = Horse('Tommy')
tom.move(34)
Go
type Animal struct {
name string
}
func (a *Animal) move(distanceInMeters int) {
fmt.Printf("%s moved %d.\n", a.name, distanceInMeters)
}
type Horse struct {
*Animal
}
func (h *Horse) move(distanceInMeters int) {
fmt.Println("Galloping...")
h.Animal.move(distanceInMeters)
}
var tom *Horse = &Horse{&Animal{name: "Tommy"}}
tom.move(34)
TypeScript
class Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Horse extends Animal {
constructor(name: string) {
super(name);
}
move(distanceInMeters = 45) {
console.log("Galloping...");
super.move(distanceInMeters);
}
}
let tom: Animal = new Horse("Tommy");
tom.move(34);