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

Fix JSON::Serializable with 2 args, and YAML::Serializable with 3 arg… #6458

Merged
merged 5 commits into from Aug 27, 2018

Conversation

kostya
Copy link
Contributor

@kostya kostya commented Jul 27, 2018

…s, fixed #6405

@@ -35,6 +35,16 @@ class JSONAttrPerson
end
end

struct JSONAttrPersonWithTwoFieldInInitialize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for single field too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is many classes with 1 arg already

@RX14
Copy link
Contributor

RX14 commented Jul 30, 2018

Please restore the * forcing them to be named arguments. It'd also be nice to rename these pull_for_json_serializable so it's almost impossible to accidentally hit these methods. These initialize methods aren't used by anything but the macro-generated .new methods so they should be as hard to call as reasonable.

@kostya
Copy link
Contributor Author

kostya commented Jul 30, 2018

i actually have some another problems with my code (inheritance and includes), after this commit, trying to understand why

This reverts commit 86bf9bd.
@RX14
Copy link
Contributor

RX14 commented Aug 8, 2018

@kostya any progress?

@kostya
Copy link
Contributor Author

kostya commented Aug 8, 2018

Bug in my project not related to this commits, it happens on crystal master also, but hard to find why. works in 0.25.1. So i think this can be merged.

instantiating 'super(JSON::PullParser)'
in /Users/kostya/projects/crystal/src/json/serialization.cr:135: instance variable '@json_unmapped' of MyClass+ was not initialized in this 'initialize', rendering it nilable

def initialize(pull : ::JSON::PullParser, dummy : Nil)
    ^~~~~~~~~~

@kostya
Copy link
Contributor Author

kostya commented Aug 8, 2018

bisect to ee6ec50, here problem with @json_unmapped appears.

@@ -138,7 +138,9 @@ module YAML
end
end

def initialize(ctx : YAML::ParseContext, node : ::YAML::Nodes::Node, dummy : Nil)
def initialize(*, context_for_yaml_serializable : YAML::ParseContext, node_for_yaml_serializable : ::YAML::Nodes::Node)
ctx = context_for_yaml_serializable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could avoid the local assignations by using external/internal naming of the parameters:

def foo(*, external_long_name name : String, external_long_age age : Int32)
  {name, age}
end

foo(external_long_name: "John", external_long_age: 30) # => {"John", 30}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What? never see this before

@@ -117,7 +117,7 @@ module JSON

def self.new(pull : ::JSON::PullParser)
instance = allocate
instance.initialize(pull, nil)
instance.initialize(pull_for_json_serializable: pull)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might I recommend the usage of double underscore to name the parameters? Macro variables get converted into __temp_1, perhaps we should use something similar to reference the private-yet-public interface?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO that's too much. There's nothing like that used in stdlib ATM.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option then will be to make initialize private, which can then only be called by this particular .new constructor 🤷‍♂️ , thus discouraging externals call to it.

@asterite
Copy link
Member

@bcardiff Maybe this would be nice to have for 0.26.1

@bcardiff bcardiff added this to the 0.26.1 milestone Aug 27, 2018
@bcardiff bcardiff merged commit 71b69a2 into crystal-lang:master Aug 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSON::Serializable causes compilation error when combined with an initialize method with exactly 2 params
6 participants