I have two examples here, and they behave different, when I think they
should not.
v1.0 compiles properly, but v.0 does not, with error: [0] cannot be found:
process_frame(…)
returns a tuple. (Note the four calls are the same, but only while trying
to solve the compile problem)
What is the case doing that changes the type of the returned value such
that it is not a tuple?
val v = case 0 of
| _ when size = u8(1) => process_frame(packetContents)
| _ when size = u8(2) => process_frame(packetContents)
| _ when size = u8(4) => process_frame(packetContents)
| _ => process_frame(packetContents)
val b = v.0
val v1 = process_frame(packetContents)
val b1 = v1.0
In ATS, the type of a case-expression (or if-expression) needs to be
specified.
For instance, it is unclear what type should be assigned to the expression
‘if test then 0 else 1’; it could be ‘int’ or ‘natLt(2)’ or something
else. If no
type is explicitly specified, the system assume that ‘if test then 0 else 1’
has some type X such that both int(0) and int(1) are subtypes of X.
In your case, this X is not specific enough to support field-selection.
You can fix this by writing:
val v = (case … of …): T
for some record type T. Note that the following style of annotation cannot
resolve
the issue:
val v: T = case … of …
as it only means that the type of the case-expression is a subtype of T and
v is given
whatever type that is given to the case-expression.On Thursday, October 8, 2015 at 11:52:23 PM UTC-4, Mike Jones wrote:
I have two examples here, and they behave different, when I think they
should not.
v1.0 compiles properly, but v.0 does not, with error: [0] cannot be found:
process_frame(…)
returns a tuple. (Note the four calls are the same, but only while trying
to solve the compile problem)
What is the case doing that changes the type of the returned value such
that it is not a tuple?
val v = case 0 of
| _ when size = u8(1) => process_frame(packetContents)
| _ when size = u8(2) => process_frame(packetContents)
| _ when size = u8(4) => process_frame(packetContents)
| _ => process_frame(packetContents)
val b = v.0
val v1 = process_frame(packetContents)
val b1 = v1.0
For the moment, intuitive understanding is enough: T1 is a subtype
of T2 if each value of T1 can also be given the type T2.On Friday, October 9, 2015 at 11:02:46 AM UTC-4, Mike Jones wrote:
I poked around for a description of subtypes, but could not find a
specific doc section on it. Is there a go to place discussing subtypes?