Comments (11)
Haha, this is a very big model. I can see why you're hitting the limits of Swift's currying. Are there some submodels you feel you can make of these? A couple ideas I have are:
All the keys starting with "total" can be moved into a Totals
model.
All of the "spells" can be moved into a Spells
model.
All of the "items" can be moved into a ItemStorage
model.
And in a similar fashion, group as many like things as you can into separate models. This might make things easier to work with too.
Another idea, if you are displaying all of these attributes to a user on some UI, maybe you can group these properties by the UI portion they will be displayed on. Say for instance you have a UI that has different views for the items the user had and a different view for the spells they used. Then you could group those into separate models. When it comes time to display the info, you can hand each view only the model it needs to display its information.
from argo.
Yep, that's one plan of attack. You could also make a case for having a struct that simply alphabetizes the properties: statsAF (all stats with variables names A-F), statsGM, etc.
The other plan: make RawStats only contain one 'let': the raw json itself, which can be extracted from a JSON enum via a switch statement. In decode, make a RawStats struct with one property, then wrap it in Decoded, and return it.
Then have all of the rest of these pieces of data - item0, item1, etc - be computed properties that simply get the value out of the JSON when requested. Yes, it moves very far away from what Argo is trying to accomplish with the Decoded type, the safety, the error messages, the guarantees of data existence, etc. But this is the only piece of data that has this issue, and I'm fine waiting for this Swift issue to go away.
from argo.
Ya, I think your second option isn't a bad idea for this. Unfortunately, Swift just can't handle so many properties on a model just yet. If you have a model that just contains the raw JSON
value then you can make computed properties like you said. You can even use Argo in the computed properties to just get that one value out of the JSON.
from argo.
In my testing the limit seems to be around 10-12 properties, which I don’t think is excessive on a model. The workaround from Swift 1.1 is broken on 1.2, and I’m struggling to work out what’s going on with the types.
Calling MyType.create <^> j <| "property1" <*> j <| "property2"
returns a Decoded<(remainder of curried create function)>
(e.g. Decoded<(property3: String) -> MyType>
)
If I assign this to a local variable, based on my limited understanding of applicatives, I should be able to continue in a new expression with: f <*> j <| "property3"
, however this doesn’t type check: Could not find an overload for '<*>' that accepts the supplied arguments
I’m not sure if my understanding is correct, or I’ve missed something obvious. At the moment I’m manually running the logic from apply
, i.e.:
switch f {
case let .Success(box):
return box.value <^> j <| "property3"
case let .MissingKey(string):
return .MissingKey(string)
case let .TypeMismatch(string):
return .TypeMismatch(string)
}
but this is really clunky and there must be an easier way.
from argo.
@samritchie Thank you for bringing this to our attention. This issue seems to have started when switching to the Decoded
type. This is most likely a bug with the compiler as we can reproduce this with Array
but not with Optional
. We'll be submitting a bug to Apple for this.
In the mean time, we found a workaround. If you add _
to your curried parameters it should work. For instance:
static func create(prop1: String)(prop2: String)(_ prop3: String) -> String {
return ""
}
let f = MyType.create <^> j <| "property1" <*> j <| "property2"
let x = f <*> j <| "property3"
Hope this helps. Going to keep this open for now. Please feel free to ask more questions about this workaround. @gfontenot is submitting a radar as we speak and will post the ID in here when he's done.
from argo.
Thanks, that works. That was an odd one - I’m relieved to know I haven’t misunderstood applicatives as much as I thought.
from argo.
The radar I submitted is available on Open Radar. The Swift team doesn't like dupes, so no point in that, but it's been reported.
from argo.
Looks like workaround with _
in curried parameters has limited functionality.
Update: It works like a charm and I should go to sleep next time I intend to write in this thread.
I have a create
function like this:
public static func create(id: String?)
(title: String?)
(address: String?)
(_ description: String?)
(_ area: Double?)
(_ price: Price?)
(_ roomsNumber: Int?)
(_ photoIds: [String]?)
(_ contacts: [String]?) -> Realty
And decode
function:
public static func decode(x: JSON) -> Decoded<J>? {
let partial = Realty.create
<^> x <|? "id"
<*> x <|? "title"
<*> x <|? "address"
let partial2 = partial
<*> x <|? "description"
<*> x <|? "area"
<*> x <|? "price"
return partial2
<*> x <|? "roomsNumber"
<*> x <|? "photoIds"
<*> x <|? "contacts"
}
And on last partial clause I'm getting Could not find an overload for '<*>' that accepts the supplied arguments
error.
from argo.
@semka the last two lines of your parser are parsing single values where your create
function expects arrays. Replace those two with <||?
and you should be good to go.
from argo.
Me so stupid, thank you very much!
from argo.
I'm going to go ahead and close this. Feel free to continue to talk in here.
from argo.
Related Issues (20)
- error: no such module 'Runes' HOT 7
- Problem with Decodable on xcode 9 HOT 3
- RFC: Renaming the Decodable protocol
- How to decode a "Dictionary" to "[Model]"? HOT 3
- Parse nested array failed HOT 5
- Parse nested model fails when adding a specific key HOT 2
- How to decode dynamic keys HOT 2
- RFC: Renaming (or removing) the `<|` family of operators HOT 5
- Decode type 'Any' HOT 2
- RFC: Make Swift.Decodable conform to Argo.Decodable in Swift 4.1 HOT 4
- Convert all wrong type return JSON to specific type HOT 6
- Decoding the same object with different keys HOT 6
- How to parse an empty dictionary object in Argo HOT 1
- [Question] Array decoding HOT 4
- Using pure can't compile using Swift 3 HOT 2
- Decodable' is ambiguous for type lookup in this context (Swift4) HOT 1
- Parsing a json array with strings and objects intermixed HOT 2
- Xcode 11.0 compatibility issue HOT 3
- Xcode 11.2.1 Binary operator '<^>' cannot be applied to operands of type HOT 7
- Upgrade to version 5.0.0 blocked due to Ogra library HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from argo.