SugarCpp icon indicating copy to clipboard operation
SugarCpp copied to clipboard

support for constant definition out of class

Open zxytim opened this issue 11 years ago • 5 comments

zxytim avatar May 24 '13 19:05 zxytim

Are you meant this?

[const]
a: int = 10

b: const int =10

[static, const]
c: int = 10

curimit avatar May 24 '13 19:05 curimit

int main()
    [const]
    a: int(5)
line 2:5 mismatched input '[' expecting DEDENT
line 4:1 missing EndOfFile at 'DEDENT'

zxytim avatar May 25 '13 05:05 zxytim

I see...The main problem is that "const" is a part of type_name not attribute when define variables.

import "iostream"

using namespace std

// define const variable out of class
a, b: const int(10)
// hint: const is not a valid attribute for variable definition

[public]
class Test
    // const modifier on type name
    const int test(x: const int) = 1

    // const attribute means const function, not return type is const
    // output: const int get() const;
    [const]
    const int get() = 1

int main()
    // Attribute can be only use out of function body
    a: const int(5)

curimit avatar May 25 '13 06:05 curimit

but in SugarCpp-Ftp:

[static, const]
    CMD_BUF_LNE := 256

which compiles to

static const decltype(256) CMD_BUF_LNE = 256;

a more test shows that (without static attribute for clearity):

class FtpServer
    [const]
    CMD_BUF_LNE : int = 256

compiles to

#pragma once

class FtpServer {
    const int CMD_BUF_LNE = 256;
};

here it clearly shows that the attribute [const] in class can be used as part of variable type, but it is not permitted inside a function like:

int main()
    [const]
    a: int = 5

The two behaviors are not consistent in my view.

zxytim avatar May 25 '13 06:05 zxytim

The attribute syntax was borrowed from C#, which means extra information that compiler can recognize. Attribute can only be used to modify these things: function/class/enum/variable definition in global scope or class scope/field in class scope. Allow using attribute in function body may reduce the readability of the code. How about keep const attribute in class scope but forbid using it in function body?(currently both forbidden) Since syntax has changed SugarCpp-Ftp maybe have to update.

curimit avatar May 25 '13 07:05 curimit