Czy w Scali można "curryować" typy o wyższym pokrewieństwie?
Załóżmy, że mam cechę o dwóch parametrach typu, np.
trait Qux[A, B]
I inna cecha o wyższym parametrze typu, np.
trait Turkle[C[_]]
Chciałbym móc zastąpić stałą wartość dla jednego z parametrów typu Qux
, aby mogła być użyta do parametryzacji Turkle
.
Oto przykład (kodu, który nie ma sensu w Scali!):
trait Baz[A] extends Turkle[Qux[A, _]]
[5]}ktoś ma jakiś pomysł jak osiągnąć ten efekt? 20
Author: Scott Morrison, 2011-06-06
4 answers
Jason Zaugg wymyślił najbardziej zwięzły sposób, aby to zrobić:
trait Baz[A] extends Turkle[({type x[a]=Qux[A, a]})#x]
Wtyczka IntelliJ Scala opcjonalnie zwija to do:
trait Baz[A] extends Turkle[x[a]=Qux[A, a]]
22
Author: Lachlan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-06 06:02:15
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-06 06:02:15
Masz na myśli coś takiego?
trait QuxWithString[A] extends Qux[A, String]
new Turkle[QuxWithString]{}
Jest to analogia do częściowego zastosowania dla typów.
3
Author: Kim Stebel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-06 05:26:17
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-06 05:26:17
trait Turkle[C[_]]
trait Qux[A,B]
trait Wraps[A] {
type Jkz[X] = Qux[A,X]
trait Baz extends Turkle[Jkz]
}
2
Author: Rex Kerr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-06 05:09:38
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-06 05:09:38
Wtyczka kompilatora kind projector pozwala również na to:
// Explicit lambda, greek letters
trait Baz[A] extends Turkle[λ[α=>Qux[A,α]]]
// Explicit lambda, normal letters
trait Baz[A] extends Turkle[Lambda[a=>Qux[A,a]]]
// No explicit lambda, ? placeholder
trait Baz[A] extends Turkle[Qux[A,?]]
0
Author: Markus1189,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-06 07:38:40
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-06 07:38:40