https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
m

mattmoore

05/11/2020, 3:34 PM
👋🏻 Welcome everyone! I'm excited to see what we can pull off with this! So I'm still catching up from yesterday's conversation, but so far I'm seeing a couple of high-level things: 1. Scala's pattern matching is nice to have for smaller parameter lists (I'm thinking of typical ADT matches returned by monads like
Some
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.
n

Nico

05/11/2020, 4:02 PM
Tiny note on (1.), you should make
_
a match on
Any?
rather than
Any
🙂
As fot
KtWhenCondition
. 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 online
r

raulraja

05/11/2020, 4:29 PM
quotes won’t help us here
for a decent plugin we need to consider what I put on the channel
there are several phases
quotes or tree is the less important one in this case
as long as you can just produce valid syntax in the Kotlin Grammar
you would like to see type errors which is where the fun begins and we can create ad-hoc relations and alter smartcasting for the branches of the when
n

Nico

05/11/2020, 4:31 PM
Right we can start by discarding the keep grammar then. It is not valid grammar in the parse tree
r

raulraja

05/11/2020, 4:36 PM
what would be the closest to something that does not type check but is grammatically correct?
for example in place of _ backticked as singleton object which we assign a special meaning to
or with a different symbol that is valid kotlin for ignoring
such as * which means in type projection DONT_CARE
n

Nico

05/11/2020, 4:40 PM
Assuming we are sticking to
when
(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
)
r

raulraja

05/11/2020, 4:41 PM
so if you change
is
for something
case
or similar
n

Nico

05/11/2020, 4:41 PM
As long as the first word of the
when
entry is not
in
or
is
, we can match on expressions in general
r

raulraja

05/11/2020, 4:41 PM
ok, then it’s gonna be easier
n

Nico

05/11/2020, 4:41 PM
case
should work yes I'll check
Yep
case(..)
works. We need the braces otherwise it's not an expression
r

raulraja

05/11/2020, 4:45 PM
it could work if it was nested in a receiver as an infix object with an invoke
n

Nico

05/11/2020, 4:45 PM
we can do
when(3) {
    case (Person(x,y)) where s -> 2
}
fine.
s
is also an expression so it could whatever too
r

raulraja

05/11/2020, 4:45 PM
but parents is good for now
n

Nico

05/11/2020, 4:46 PM
I am using the antlr grammar checker, it does not like non parenthesis
when(3) {
    case Person(x,y) where s -> 2
}
this is invalid
r

raulraja

05/11/2020, 4:46 PM
yes with where is ambiguous
what about without the where?
n

Nico

05/11/2020, 4:47 PM
Doesn't work wither. The problem is Person, it's just not expecting an identifier
r

raulraja

05/11/2020, 4:47 PM
I see
thanks for trying that
n

Nico

05/11/2020, 4:49 PM
No worries. I had the anltr grammar set up because I was planning to write a toy transpiler for the keep. So we can test the grammar and see how the stuff gets resolved to find it in the ast later
r

raulraja

05/11/2020, 4:51 PM
The arrow meta quotes give you already and ADT for kotlin and kastree also has one
we depend on a modified kastree
that keeps a connection the psi element
n

Nico

05/11/2020, 4:56 PM
For some reason this is valid
when(3) {
    case Person[y,3,Person[x,y]] where 2 -> 2
}
ie, square brackets apparently are a collection literal which is an atomicExpression
r

raulraja

05/11/2020, 5:00 PM
woooo
now we are talking
the semantics are also of operator access which is what is doing in the nested structure
it’s a polyadic version of the unsafe get but it wouldn’t be unsafe in matching since it’s a partial function
n

Nico

05/11/2020, 5:03 PM
I agree semantically it kind of makes sense. This is also valid
when(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 condition
r

raulraja

05/11/2020, 5:03 PM
right that is excelent
can you use || as disjunction opoerator?
or | ?
also the the semantics of where as expression here are in sync with those of the lang. where is used for sub type upper bounds but here is a range of inhabitants of a type based on the match so it’s the same semantics but for the runtime
n

Nico

05/11/2020, 5:05 PM
You can use
||
:)) 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 language
r

raulraja

05/11/2020, 5:06 PM
comma represents a product so both cases
in argument position always
so it represents the opposite of the coproduct
n

Nico

05/11/2020, 5:06 PM
Is it not a sum in a
when
??
r

raulraja

05/11/2020, 5:06 PM
coproduct in kotlin is sealed hierarchy, enumeration or the upcoming union types
oh you mean for
is
?
n

Nico

05/11/2020, 5:07 PM
wait did a miss something what do you mean upcoming are union types coming?? In the lang I mean
Yeah in the
is
case it's a disjunction
r

raulraja

05/11/2020, 5:08 PM
i didn’t know that thanks
in that case is consistent
yes, I have union types working in kotlin and other crazy stuff
refined types, SMT solving, structural polymorphism, cohercion functions and other stuff people have not seen yet that is comming to meta
most of them come with KEEP this year but not until we test them in Arrow and we cover all the edge cases.
n

Nico

05/11/2020, 5:10 PM
so many types ahahah nice
r

raulraja

05/11/2020, 5:11 PM
do you mind sharing the error with the braces?
I want to see where it is chocking type checking or wherever
n

Nico

05/11/2020, 5:11 PM
Yep 1min
It's an antlr error so it's a parse error I'm afraid
line 2:9 mismatched input 'Person' expecting {NL, ',', '->'}
that's the antlr error 🙂
For this:
when(3) {
    case Person(2,3) -> 2
}
r

raulraja

05/11/2020, 5:14 PM
I mean for your case sorry meant to say square brackets
the one that finds it as a valid expression in grammar
n

Nico

05/11/2020, 5:17 PM
oh ok
unresolved identifier for
case
obviously
Unsupported [Collection literals outside of annotations] for the square braces
And unresolved reference
x
that's all. It's for:
val x = when(3) {
      case Person[x] -> 2
      else -> 23
    }
so it's a when expression
the reason for collection literals in the grammar makes more sense now ahahah
r

raulraja

05/11/2020, 5:32 PM
totally
Here is some code that gets to typechecking
n

Nico

05/11/2020, 9:46 PM
What happened to the idea of using property accessing syntax?
It looks like it will be work but it looks preetty good
r

raulraja

05/11/2020, 10:12 PM
It's compatible too if we want to auto project the optics DSL
But if you want underscore and Scala like destructuring then it looks like we can make it work
Or you mean [ ]
That can work too
n

Nico

05/12/2020, 8:35 AM
Underscore is great but I meant [ ] because it allows
case [...], case Person[...]
instead of
case((..)), case(Person(..))
, saving us from a pair of brackets. But i don't mind semantics are the same now.