2/29/2024 0 Comments Python unpacking argumentsI’d want it to unpack a foo(**myFoo) if-and-only-if myFoo is of a case class defined as: case class Foo(override val toString: String, override val hashCode: Int)ĭoing it implicitly based on #toString or java.lang. Um, why not? def foo(toString: String, hashCode: Int) = ? seems awfully suggestive, no? What else would you want it to do? case classes give us both these things, but with a bit of work it could certainly be generalized a bit we don’t want to accidentally be unpacking things like toString or hashCode. The important properties are that (a) it’s statically typed and (b) the set of “known fields” is well defined, e.g. This is basically the Target-Typed New from C# Author author = new("Matt Eland") // Note: Author not repeated for the constructor ![]() The syntax is awful (it means the exact opposite of what most people would expect. On the other hand, the other example you gave: bar(.(i = 0, k = "zero")) // Explicit request for boxing or **, but at least that only becomes a problem for new code with the marker, whereas trying to make it fully implicit will cause ambiguity in existing code. This is also a problem with an explicit marker. Where the i from Things and the i in the parameter list next to it will collide. I don’t think being this permissive will work, because you then start getting into issues like def bar(things: Things, i: Int) = ? Ichoran: bar(i = 0, k = "zero") // Fully implicit-is this okay? ) via apply on a companion or a constructor, and to any accessor (whether a method or extension method) t.a: A with the type needed. becomes the universal “fill in things by keyword” prefix.ĭepending on how expansive we wanted to be, we could generalize beyond case classes to any type T that can create an instance using T(a = x. ![]() If, like Rust, we introduce a Default trait, we could interpret any missing parts as missing = summon].missing, so partial matches would be okay.Īnyway, the key idea here is that. If we can do the same thing as a language feature, it could be much less noisy: def foo[T Int = īut stuff like def baz(c: Char, s: String) = ? would still be entirely free of case classes. Bunch of handwaving follows (been a while since I’ve done shapeless), but this should be sort of close to what could be done in Scala 2, so settling for a Map feels like defeat.įoo("name" -> "E.T." :: "year" -> 1982 :: HNil). ![]() A technique for compressing multiple function arguments into a.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |