我可以轻松地将我的元组类型同义词作为Read的实例吗?

时间:2022-06-26 22:50:17

I have a type synonym declared for the tuple of two Ints:

我为两个Int的元组声明了一个类型同义词:

type Coordinate = (Int, Int)

I'd like to read in Coordinates as part of an IO Action, which boils down to using the read function. Is there a way to leverage the existing Read instance of Tuples to accomplish this?

我想在坐标系中读取IO动作的一部分,归结为使用读取功能。有没有办法利用现有的元组读取实例来实现这一目标?

2 个解决方案

#1


type Coordinate = (Int, Int)
home = read "(1,2)" :: Coordinate
school = read "(10,4)" :: Coordinate

#2


Have you just tried it?

你刚试过吗?

The answer is: yes, the tuple instance is used, there's no need to do anything at all yourself because Coordinate isn't even a different type – it's just a “hard link” to the same type (Int,Int), with all the same functions and class instances.

答案是:是的,使用了元组实例,没有必要自己做任何事情因为Coordinate甚至不是一个不同的类型 - 它只是一个相同类型(Int,Int)的“硬链接”,所有相同的函数和类实例。

#1


type Coordinate = (Int, Int)
home = read "(1,2)" :: Coordinate
school = read "(10,4)" :: Coordinate

#2


Have you just tried it?

你刚试过吗?

The answer is: yes, the tuple instance is used, there's no need to do anything at all yourself because Coordinate isn't even a different type – it's just a “hard link” to the same type (Int,Int), with all the same functions and class instances.

答案是:是的,使用了元组实例,没有必要自己做任何事情因为Coordinate甚至不是一个不同的类型 - 它只是一个相同类型(Int,Int)的“硬链接”,所有相同的函数和类实例。