Carp/core/Unit.carp

36 lines
772 B
Plaintext
Raw Permalink Normal View History

2021-07-05 15:48:35 +03:00
(doc Unit "is the empty type, also represented as `()` and equivalent to `void`
in C.")
More Unit type member enhancements (#986) * Emit/Deftype: Add a few more special cases for Unit members There were a few gaps remaining in our handling of Unit types as members of user defined types, most notably, generic setters weren't handling Units appropriately. This commit adds special checks for Unit types in generic setters, and also accounts for a Unit refs: - Specialize generic setter functions against Unit members - Specialize calls to str/prn against Unit members in StructUtils (rather than accessing the struct member, which doesn't exist in the Unit case, we simple call Unit's prn function (added in the next commit). - Don't bind references to Unit values to variables. This fixes an error whereby a reference to a Unit would generate invalid C such as: `void* _9 = &;` Likewise, we omit references to Units from function arguments just as we omit Unit values. * Unit: Add Unit type implementations for common interfaces. Now that Unit can be used as a member type it is subject to several interfaces, such as prn, that it previously hadn't implemented. This commit adds Unit.carp to core which implements `prn`, `copy`, and `zero` for the Unit type. * Deftype: Return null pointers for Unit getters This is *hopefully* one of the final updates needed to fully support Unit's as member types. Getters for fields of such types have no struct member to read, but are expected to return a void pointer; so we return a NULL void pointer instead. This commit also updates our emissions for function calls to prevent assigning the results of functions with Unit and (Ref Unit) return types to variables. * Emit: Filter void args from lambda calls Just as we filter void argument types from other function calls, we now filter them from calls to lambdas.
2020-11-21 07:57:03 +03:00
(defmodule Unit
(implements prn prn)
(sig prn (Fn [Unit] String))
(defn prn [unit]
@"()")
(doc copy
"'copies' a reference to a Unit value."
"This function just returns a fresh value of type Unit.")
(implements copy copy)
(sig copy (Fn [(Ref Unit)] Unit))
(defn copy [unit-ref]
())
(doc zero
"Returns a fresh value of type Unit (this value performs no side-effects).")
(implements zero zero)
(sig zero (Fn [] Unit))
(defn zero []
())
2020-11-24 21:31:53 +03:00
(implements = =)
(sig = (Fn [Unit Unit] Bool))
(defn = [unit-a unit-b]
true)
)
(defmodule UnitRef
(implements = =)
(sig = (Fn [&Unit &Unit] Bool))
(defn = [unit-a unit-b]
true)
More Unit type member enhancements (#986) * Emit/Deftype: Add a few more special cases for Unit members There were a few gaps remaining in our handling of Unit types as members of user defined types, most notably, generic setters weren't handling Units appropriately. This commit adds special checks for Unit types in generic setters, and also accounts for a Unit refs: - Specialize generic setter functions against Unit members - Specialize calls to str/prn against Unit members in StructUtils (rather than accessing the struct member, which doesn't exist in the Unit case, we simple call Unit's prn function (added in the next commit). - Don't bind references to Unit values to variables. This fixes an error whereby a reference to a Unit would generate invalid C such as: `void* _9 = &;` Likewise, we omit references to Units from function arguments just as we omit Unit values. * Unit: Add Unit type implementations for common interfaces. Now that Unit can be used as a member type it is subject to several interfaces, such as prn, that it previously hadn't implemented. This commit adds Unit.carp to core which implements `prn`, `copy`, and `zero` for the Unit type. * Deftype: Return null pointers for Unit getters This is *hopefully* one of the final updates needed to fully support Unit's as member types. Getters for fields of such types have no struct member to read, but are expected to return a void pointer; so we return a NULL void pointer instead. This commit also updates our emissions for function calls to prevent assigning the results of functions with Unit and (Ref Unit) return types to variables. * Emit: Filter void args from lambda calls Just as we filter void argument types from other function calls, we now filter them from calls to lambdas.
2020-11-21 07:57:03 +03:00
)