ID:2487161
 
Applies to:
Status: Open

Issue hasn't been assigned a status value.
A bit from the type-safety/syntax sugar wishlist.
// Evaluates to [datum] if datum is of type [type],
// otherwise evaluates to null.

astype(datum, type)

// or, as in C#:

datum as type

Before:
if(istype(datum, /type))
var/type/casted = datum
casted.DoStuff()

After:
var/type/casted = astype(datum, /type)
if(casted)
casted.DoStuff()

// or this:
var/type/casted = astype(datum, /type)
casted?.DoStuff()

// maybe with var type defaulting:
var/type/casted = astype(datum)
casted?.DoStuff()

// or this (3), but with type-aware member access:
astype(datum, /type)?.DoStuff()

// or C#-style:
(datum as /type)?.DoStuff()

Currently, we can do the above with:
proc
astype(datum, type)
return istype(datum, type) ? datum : null

However, it would be very nice if (3) could be done with type safety, since currently ./?. behaves as :/?: for non-var expressions, which are untypable.

Related:

- new/type().DoStuff() should also be made type-aware. It should be unambiguous enough to the compiler, right?

- optional return type annotation of procs (and operators), enabling type-checking for . and ?. on non-var expressions project-wide (so "new" would be internally "type-annotated" to the type being instantiated, along with the rest of the built-in stuff eventually). (This is already being done using external tools by tgstation)