Feature request: Value filter

What do you think about the possibility to limit values for a specific variable? Instead of just setting Int as return type, you could set Int with limitation to range 0…200 or specify exact values allowed, like 0, 2, 5 etc. Of course you could make specific types, but that’s also often harder to work with as you then need to go through an extra property to access the value, and it’s also not often being done. If you want a percentage 0.0…1.0, that’s normally just a Double, not a Percentage. If you made a Percentage type, you would need to go through percentage.value or something in order to access the actual Double value, which makes things more verbose.

I think it would be nice to add extra info about a variable or function return value in addition to just the type. This could be called value filters: Each type could have specific allowed filters. But how to express this in code. It would be slightly verbose to write something like

val percentage: Double { allowedRange: 0.0..1.0 } = 0.5

A possibility is to hide the filtering in the IDE, or just store filtering outside the actual code file, and then have to right click on the variable and add filtering.

When a is assigned to b, b will need to have filterers compatible with a.

What do you think?

This can easily be done with delegates.

val percentage by filteredValue(0.5){ it in 0.0..1.0 }

All you need to do is create the necessary Delegate class, see: https://kotlinlang.org/docs/reference/delegated-properties.html

1 Like

You may be interested in vetoable - Kotlin Programming Language

1 Like