surrealdb / surrealdb

A scalable, distributed, collaborative, document-graph database, for the realtime web
https://surrealdb.com
Other
27.34k stars 889 forks source link

Feature: Short IFs #4521

Open LucyEgan opened 2 months ago

LucyEgan commented 2 months ago

Is your feature request related to a problem?

Currently when doing a quick condition based on a truly/falsely value you need to do using the full IF. For example DEFINE FIELD ... TYPE option<point> VALUE IF ( location ) { <point> [location.lat, location.long] } ELSE { NONE } It would be nice to have location ? <point> [location.lat, location.long] : NONE as in other languages, but purely from a sugar point of view

Describe the solution

Add short IFs as in other languages condition ? trulyValue : falselyValue

Alternative methods

While not as clear it does get the job done better than a full IF

(true AND "Whatever") OR "Something";
(false AND "Whatever") OR "Something";

Supplied by @dbaudisch

SurrealDB version

2.0.0-alpha.8 for linux on x86_64

Contact Details

lucy.e@aspirecomps.co.uk

Is there an existing issue for this?

Code of Conduct

dbaudisch commented 2 months ago

The behaviour can already be achieved via

(true AND "Whatever") OR "Something";
(false AND "Whatever") OR "Something";

But developers coming from other languages are probably already familiar with the ternary expression, thus it might make sense to offer that as well.

LucyEgan commented 2 months ago

Thanks @dbaudisch ive added above under alt methods