106
General Discussion / Re: Overview of syntax
« on: November 10, 2018, 06:06:20 PM »
I've worked quite a bit in Ruby. If found the lack of ";" makes single line conditionals and loops less nice to work with.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
std::vector<std::shared_ptr<Foo>> x = foo();
return x.size() > 0 ? x[0] : std::shared_ptr<Foo>();
auto x = foo();
return x.size() > 0 ? x[0] : std::shared_ptr<Foo>();
Foo *x[] = foo();
return x.size() > 0 ? x[0] : NULL;
auto x = foo();
return x.size() > 0 ? x[0] : NULL;
int do_something()
{
defer_list __defer_list {}; // Always at top
... code ...
__defer_list.push_back([foo] { close(foo); }); // defer { close(foo) } in Go
... code ...
return 0;
}
defer_list::push_back(std::function<void()> &closure) { entries.push_back(closure); }
~defer_list() {
for (auto &defer_entry : entries) {
defer_entry();
}
}
func main() {
for i:= 0; i < 3; i++ {
defer fmt.Println(i)
}
fmt.Println("foo")
}
foo
2
1
0
int foo(int x) { return x + x; }
int bar(int x) { return x * x; }
int baz(int x) { return x * x + x; }
static inline int dispatch_on_type(enum Foobar foobar, int x) {
static int (*foobars[3])(int) = { foo, bar, baz };
return foobars[foobar](x);
}
int foo(int x) { ... }
int bar(int x) { ... }
int baz(int x) { ... }
static inline int dispatch_on_type(enum Foobar foobar, int x) {
static void **foobars[3] = { &&foo, &&bar, &&baz };
goto *foobars[foobar];
foo:
return x + x;
bar:
return x * x;
baz:
return x * x + x;
}
type Foo struct {
i32 a;
f64 b;
}
func Foo getFoo() { ... }
Foo f = getFoo();
i32 a = f.a;
f64 b = f.b;
i32 a; f64 b; Foo f;
{ a, b } = f; // or (a, b) = f, struct { a, b } = f, or whatever syntax we pick.
f = { .a = a, .b = b };
f = { a, b };
{ a, b } = f;
{ a = .b, b = .b } = f; // <- placeholder syntax
switch (a) {
case 1 | // Using : instead of | is syntax sugar for case 1: fallthrough;
case 2 |
case 3:
foo(); // With : we get a break;
case 4 |
case 5:
bar();
fallthrough; // If we want to execute a statement then pass to next.
case 6:
baz();
}
// The above in C:
switch (a) {
case 1:
case 2:
case 3:
foo();
break;
case 4:
case 5:
bar();
case 6:
baz();
break;
}
switch (foo) {
case 1|:
case 2|:
case 3:
do_something();
const i32 foo_one = 31;
func void foo()
{
i32[foo_one + 1] array = {
[foo_one] = 1;
}
...
}
func void foo(i32 one)
{
i32[one + 1] array = {
[one] = 1;
}
...
}
func void foo(i32 one)
{
i32[one + 1] array;
array[one] = 1;
...
}
type Foo struct {
...
}
const Foo.pi = 3.0;
...
func f64 Foo.pie_c(f64 r) { return Foo.pi * r * 2; }
#define types \
X(i8, 8, true) \
X(u8, 8, false) \
X(i16, 16, true) \
X(u16, 16, false) \
/* ... etc */
bool isSigned(type t) {
switch (t) {
#define X(a, b, c) case a: return c;
types;
#undef X
}
type enum State [char*(int) action] i32 {
START(startFunction) = 0,
END(endFunction)
}
state.action(10); // Calls startFunction(10)