Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct and class support #1

Open
jcd opened this issue Mar 4, 2016 · 5 comments
Open

Struct and class support #1

jcd opened this issue Mar 4, 2016 · 5 comments

Comments

@jcd
Copy link

jcd commented Mar 4, 2016

struct Foo {
   string name;
   string age;
}

Foo f = json.get!Foo(myjson);

also works the other way around

auto f = Foo("myname", 12);
json2.put("myfoo", f);

Maybe even introduce a UDA for optional fields:

struct Foo {
   string name;
   @optional string age;
}


@trikko
Copy link
Member

trikko commented Mar 4, 2016

Maybe:

struct Foo {
   @bind("renamed") 
   string _blah;

   @bind string name;
   @optional string age;

   int ignored;
}

?

@trikko
Copy link
Member

trikko commented Mar 4, 2016

Or better:

struct Foo {
   @bind("renamed") 
   string _blah;

   @bind!int string test; // On json this is a int
   @bind string name;
   @optional string age;

   int ignored;
}

to support get and as

@jcd
Copy link
Author

jcd commented Mar 4, 2016

I think that binding should be default for a field. Maybe that is the default and could be changed with a struct UDA:

struct Foo {
   @bind("renamed") 
   string _blah;

   string name;
   @optional string age;

   @ignored
   int ignored;
}

and

@bind!false
struct Foo {
   @bind("renamed") 
   string _blah;

   @bind string name;
   @optional string age;

   int ignored;
}

not sure...just an idea.

@trikko
Copy link
Member

trikko commented Mar 4, 2016

What i don't like about bound-by-default is that - for example - if you add a new property it is bound automatically and probably you don't remember this (in your example all annotations are optionals).

Maybe this sounds better.

Version for impavid:

@autobind
struct Foo{ ... }

Safer way:

struct Foo
{
    @bind("user_age") @default(18)   // If there's a default it's optional (and viceversa)
    int age; 
}

@jcd
Copy link
Author

jcd commented Mar 5, 2016

You don't like bound-by-default because it might brake existing code not supporting a newly added field? You got a point with that. It could be fixed by making all fields @optional by default. Keeping the @Bind for fields that must be there and have a new @ignore for fields that should be ignored.

That would make all structs/classes work out of the box with no annotations but could be annotated to make thing more specified if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants