path-dependent types in Scala

0
Gabriel Claramunt @gclaramunt

@jco @travisbrown def hey(t: Thing): Thing#Out = Wrapper(t).get compiles, but have't checked if that's the fix :)

2015-11-07 00:56:29
Travis Brown @travisbrown

@gclaramunt That doesn't give you a useful static type, though. You're not willing to put a type parameter on `Wrapper`, @jco?

2015-11-07 01:01:05
empanada linguist @jco

@travisbrown @gclaramunt I might be able to swing that...still don't get why it can't tell the two are equal?

2015-11-07 01:14:45
empanada linguist @jco

@econlon @travisbrown the real example this is mined from becomes much nastier if it has to have that

2015-11-07 01:15:07
Gabriella Gonzalez @GabriellaG439

@econlon @jco @travisbrown Like Eric said, you need to parametrize your class on the type it holds.

2015-11-07 01:15:43
good tweet eric @goodtweeteric

@jco @travisbrown burn it all, just get rid of inheritance and cast to Any everywhere

2015-11-07 01:15:50
Gabriella Gonzalez @GabriellaG439

@econlon @jco @travisbrown Contrary to what you think your example code is not safe

2015-11-07 01:16:57
Travis Brown @travisbrown

@GabrielG439 @econlon @jco It's possible to make it work without a type parameter—you just have to be careful not to lose the refinement.

2015-11-07 01:17:07
empanada linguist @jco

@GabrielG439 @econlon @travisbrown this is highly undesirable b/c of type inference though, and in many cases you know that Out is fixed

2015-11-07 01:21:07
Gabriella Gonzalez @GabriellaG439

@jco @econlon @travisbrown The type of your "hey" function is basically equivalent to: forall a. Thing => a

2015-11-07 01:21:08
Gabriella Gonzalez @GabriellaG439

@jco @econlon @travisbrown If Out really is fixed then you need to prove that by actually fixing it at the point of definition

2015-11-07 01:23:15
empanada linguist @jco

@GabrielG439 @econlon @travisbrown how is this unsafe? just because it doesn't seem useful doesn't make it unsafe?

2015-11-07 01:24:13
Gabriella Gonzalez @GabriellaG439

@jco @econlon @travisbrown Parametrizing it on the wrapped type should improve type inference

2015-11-07 01:24:25
Gabriella Gonzalez @GabriellaG439

@jco @econlon @travisbrown t.Out is existentially quantified, which means that you can use it, but only in a restricted way

2015-11-07 01:26:37
Gabriella Gonzalez @GabriellaG439

@jco @econlon @travisbrown This is hard to explain in one tweet, so tweet storm incoming...

2015-11-07 01:28:29
Travis Brown @travisbrown

@GabrielG439 @jco @econlon Not exactly true, thanks to Scala's path-dependent types (assuming the compiler statically knows `t`'s `Out`).

2015-11-07 01:29:02
Gabriella Gonzalez @GabriellaG439

@jco @econlon @travisbrown For an existentially quantified type you can only confirm it's consistent with other fields of the same class

2015-11-07 01:31:11
Gabriella Gonzalez @GabriellaG439

@travisbrown @jco @econlon Right, this is the same thing as existential quantification. You can know the type within a pattern match

2015-11-07 01:33:03
good tweet eric @goodtweeteric

@GabrielG439 @jco @travisbrown the classes end up being huge modules in that case, so probably not too bad

2015-11-07 01:33:05
Gabriella Gonzalez @GabriellaG439

@travisbrown @jco @econlon But the type cannot leak from that scope without a type error

2015-11-07 01:33:27