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

Support for Tuple #20

Closed
wants to merge 9 commits into from
Closed

Support for Tuple #20

wants to merge 9 commits into from

Conversation

jacereda
Copy link
Collaborator

@jacereda jacereda commented Oct 6, 2017

No description provided.

a <- readImpl $ get arr 0
b <- readImpl $ get arr 1
pure $ Tuple a b
l -> fail $ TypeMismatch "2 values" (show l <> " values")
Copy link
Owner

Choose a reason for hiding this comment

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

Can't this provide more information?..

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What would you suggest?

instance readForeignTuple :: (ReadForeign a, ReadForeign b) => ReadForeign (Tuple a b) where
readImpl = asTuple <=< readArray
where asTuple :: Array Foreign -> F (Tuple a b)
asTuple arr = case length arr of
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this code would be simpler by pattern matching on arr

asTuple = case _ of
  [a, b] -> ...
  _ -> ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Of course, thanks.

ra <- readImpl a
rb <- readImpl b
pure $ Tuple ra rb
l -> fail $ TypeMismatch "2 values" (show (length l) <> " values")
Copy link
Owner

Choose a reason for hiding this comment

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

Oh wait, I just realized now that you have a single array decoding. For reading, is there not a way we could somehow maybe read in the whole array and then map them back to the tuples? Might require some generics-rep code?

Copy link
Owner

Choose a reason for hiding this comment

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

Like (a /\ b /\ c /\ d) should be [a,b,c,d]?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good. Separate PR?

Copy link
Owner

Choose a reason for hiding this comment

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

Realized that some actually do encode nested array pairs though. I think probably it'd be best to go with whatever you need with this for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, nested tuples don't serialise correctly, I'll try to fix it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure I understood what you mean... I need ReadForeign Unit for encoding Tuple3 & friends and used a broken readImpl for now, but I guess Data.Foreign could provide that.

Copy link
Owner

Choose a reason for hiding this comment

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

You should never be using the value of Unit anywhere for reading or writing, it's only passed around like an opaque foreign import data declared type. You should always use an empty record for this.

Copy link
Owner

Choose a reason for hiding this comment

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

Unit's foreign representation should be null or undefined or something anyway, the value is absolutely meaningless because it's never supposed to be inspected or used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is that what you mean? How would that be better?
In any case, a generic n-tuple serialisation as array probably won't need unit anyway, right?

Copy link
Owner

Choose a reason for hiding this comment

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

Well, as in no one will give you a stop element anyway. People expect (1 /\ 2 /\ 3) to become [1,2,3]

@@ -182,7 +182,7 @@ instance writeForeignForeign :: WriteForeign Foreign where
writeImpl = id

instance writeForeignUnit :: WriteForeign Unit where
writeImpl = toForeign
writeImpl = const $ writeImpl {}
Copy link
Owner

Choose a reason for hiding this comment

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

Please don't do this, this is not supposed to work in any way.

@@ -182,7 +182,7 @@ instance writeForeignForeign :: WriteForeign Foreign where
writeImpl = id

instance writeForeignUnit :: WriteForeign Unit where
Copy link
Owner

Choose a reason for hiding this comment

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

This instance should not exist at all, there is not valid value for Unit. This will also break in any non-JS backend like purerl.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Then we'd need to use -generics-rep, right?

test/Main.purs Outdated
{ "a": [1, "foo"] }
"""
it "works with nested Tuple" $ roundtrips (Proxy :: Proxy MyTestNestedTuple) """
{ "a": [1, ["bar", 4.2]] }
Copy link
Owner

Choose a reason for hiding this comment

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

Test case here should be { "a": [1, "bar", 4.2] }

Copy link
Owner

Choose a reason for hiding this comment

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

ah, sorry, this comment is wrong anyway

Copy link
Owner

Choose a reason for hiding this comment

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

or there's really no sane way we can try to solve nested tuples and flatten them, it seems

Copy link
Collaborator Author

@jacereda jacereda Oct 9, 2017

Choose a reason for hiding this comment

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

Should I just close this PR? Looks like there're easy workarounds, e.g, this should serialise as [x,y,z] properly:

data V3 = V3 Number Number Number

derive instance gV3 :: Generic V3 _ 
instance writeForeignV3 :: WriteForeign V3 where 
  writeImpl = genericEncode $ defaultOptions { unwrapSingleConstructors = true }
...

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, we could. If you need the simple pair tuple case we should put it in though.

test/Main.purs Outdated
{ "a": [1, ["bar", 4.2]] }
"""
it "works with Tuple3" $ roundtrips (Proxy :: Proxy MyTestTuple3) """
{ "a": [1, ["bar", [4.2, {}]]] }
Copy link
Owner

Choose a reason for hiding this comment

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

Test case here should be { "a": [1, "bar", 4.2] }

@justinwoo
Copy link
Owner

Closing this since we can't really represent tuples in a non-magic way...

@justinwoo justinwoo closed this Mar 16, 2018
@ursi ursi mentioned this pull request Jan 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants