mattmoore
05/11/2020, 3:34 PMSome
or None
. However, for constructor matches with lots of parameters, this can be confusing when there are similar types. Honestly, it's something in Scala that I've just dealt with but now that Raul has called attention to that, I agree. It would be nice to offer something a little bit easier to deal with. I like Raul's suggestion of making use of lenses for this. I feel we could offer both, as Nico pointed out. For starters, from a compiler plugin perspective, I'm thinking of starting with supporting at least a few basic Scala-style pattern matches using their constructor-styled wildcards, and type constraints (Person(firstName = "Matt", lastName = String)
) or something similar to that. Of course you could just do it without named parameters Person("Matt", String)
. For wildcard matching, if using _
, what do you think about essentially translating that to Any
? Or when using as a wildcard for a given field, would you expect that to match the type of the field? I'm thinking for a standalone wildcard (no class constructor) just rewrite _
as Any
but if it's in a class constructor, look up the type of the field and constrain the param to that type.
2. I like the use of guards. Regarding partial matches though, I don't yet have any ideas about how that would get implemented as a plugin. @raulraja correct me if I'm wrong, but I can still use Meta to at least get the when
expression, but I'm not yet sure just how deeply I can check individual types on parameters. In my limited time looking into Meta, it appears most (all?) nodes are accessible, so I'm hoping I can check the type of a given value through the AST node? I'm also not sure I understand yet the structure of KtWhenCondition
on a given KtWhenEntry
. It's a collection, but there seems to only be a single condition attached even though I have multiple conditions...I'm probably not understanding what the breakdown is though.Nico
05/11/2020, 4:02 PM_
a match on Any?
rather than Any
🙂Nico
05/11/2020, 4:04 PMKtWhenCondition
. I really do not know what you are looking at, but that looks like the grammar names. So my guess is you are staring at the AST or more likely the parse tree. For an idea on what nodes have what children, I suggest you take a look at the official grammar. https://kotlinlang.org/docs/reference/grammar.html
It's in antlr's flavour of BNF. Let me know if you need help reading it but there should be plenty of resources onlineraulraja
05/11/2020, 4:29 PMraulraja
05/11/2020, 4:29 PMraulraja
05/11/2020, 4:29 PMraulraja
05/11/2020, 4:29 PMraulraja
05/11/2020, 4:30 PMraulraja
05/11/2020, 4:30 PMNico
05/11/2020, 4:31 PMraulraja
05/11/2020, 4:36 PMraulraja
05/11/2020, 4:36 PMraulraja
05/11/2020, 4:36 PMraulraja
05/11/2020, 4:37 PMNico
05/11/2020, 4:40 PMwhen
(otherwise anything is possible?) if we drop the keyword is
we can do much more, because that's what causes a match with the typecheck
node which is just a hardocded inOperator type
(ie, is Person
)raulraja
05/11/2020, 4:41 PMis
for something case
or similarNico
05/11/2020, 4:41 PMwhen
entry is not in
or is
, we can match on expressions in generalraulraja
05/11/2020, 4:41 PMNico
05/11/2020, 4:41 PMcase
should work yes I'll checkNico
05/11/2020, 4:42 PMcase(..)
works. We need the braces otherwise it's not an expressionraulraja
05/11/2020, 4:45 PMNico
05/11/2020, 4:45 PMwhen(3) {
case (Person(x,y)) where s -> 2
}
fine. s
is also an expression so it could whatever tooraulraja
05/11/2020, 4:45 PMNico
05/11/2020, 4:46 PMNico
05/11/2020, 4:46 PMwhen(3) {
case Person(x,y) where s -> 2
}
this is invalidraulraja
05/11/2020, 4:46 PMraulraja
05/11/2020, 4:47 PMNico
05/11/2020, 4:47 PMraulraja
05/11/2020, 4:47 PMraulraja
05/11/2020, 4:47 PMNico
05/11/2020, 4:49 PMraulraja
05/11/2020, 4:51 PMraulraja
05/11/2020, 4:51 PMraulraja
05/11/2020, 4:51 PMNico
05/11/2020, 4:56 PMwhen(3) {
case Person[y,3,Person[x,y]] where 2 -> 2
}
ie, square brackets apparently are a collection literal which is an atomicExpressionraulraja
05/11/2020, 5:00 PMraulraja
05/11/2020, 5:01 PMraulraja
05/11/2020, 5:01 PMraulraja
05/11/2020, 5:02 PMNico
05/11/2020, 5:03 PMwhen(3) {
case Person[y,3,Person[x,y]], case Person[2] where 2 -> 2
}
So we can make disjunctions a thing, which are the existing semantics of having a comma in a when conditionraulraja
05/11/2020, 5:03 PMraulraja
05/11/2020, 5:04 PMraulraja
05/11/2020, 5:04 PMraulraja
05/11/2020, 5:05 PMNico
05/11/2020, 5:05 PM||
:)) but personally ngl I think a comma is good. Think of:
when(alice) {
is Person, Alien -> ...
is Zombie, in 2..3 -> ...
}
a comma already represents a disjunction in the languageraulraja
05/11/2020, 5:06 PMraulraja
05/11/2020, 5:06 PMraulraja
05/11/2020, 5:06 PMNico
05/11/2020, 5:06 PMwhen
??raulraja
05/11/2020, 5:06 PMraulraja
05/11/2020, 5:07 PMis
?Nico
05/11/2020, 5:07 PMNico
05/11/2020, 5:08 PMis
case it's a disjunctionraulraja
05/11/2020, 5:08 PMraulraja
05/11/2020, 5:08 PMraulraja
05/11/2020, 5:08 PMraulraja
05/11/2020, 5:09 PMraulraja
05/11/2020, 5:10 PMNico
05/11/2020, 5:10 PMraulraja
05/11/2020, 5:11 PMraulraja
05/11/2020, 5:11 PMNico
05/11/2020, 5:11 PMNico
05/11/2020, 5:12 PMNico
05/11/2020, 5:12 PMline 2:9 mismatched input 'Person' expecting {NL, ',', '->'}
that's the antlr error 🙂Nico
05/11/2020, 5:13 PMwhen(3) {
case Person(2,3) -> 2
}
raulraja
05/11/2020, 5:14 PMraulraja
05/11/2020, 5:14 PMNico
05/11/2020, 5:17 PMNico
05/11/2020, 5:17 PMcase
obviouslyNico
05/11/2020, 5:18 PMNico
05/11/2020, 5:18 PMx
that's all. It's for:
val x = when(3) {
case Person[x] -> 2
else -> 23
}
so it's a when expressionNico
05/11/2020, 5:19 PMraulraja
05/11/2020, 5:32 PMraulraja
05/11/2020, 5:33 PMNico
05/11/2020, 9:46 PMNico
05/11/2020, 9:47 PMraulraja
05/11/2020, 10:12 PMraulraja
05/11/2020, 10:13 PMraulraja
05/11/2020, 10:15 PMraulraja
05/11/2020, 10:15 PMNico
05/12/2020, 8:35 AMcase [...], case Person[...]
instead of case((..)), case(Person(..))
, saving us from a pair of brackets. But i don't mind semantics are the same now.