Changeset 8e4f34e


Ignore:
Timestamp:
Dec 10, 2024, 2:28:31 PM (8 days ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
6e6e372
Parents:
fd0a1799
Message:

Allow builtin ++ from += overloads, and similar, to work on a type witout a parameterless constructor

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/prelude/builtins.c

    rfd0a1799 r8e4f34e  
    7676}
    7777
     78// Nearly "otype," except no parameterless constructor.
     79// All you need, to store values in variables and to pass and return by value.
     80// Many cases of
     81//     forall( T ...
     82// can be "simplified" to
     83//     forall( T& | is_value(T) ...
     84forall( T* )
     85trait is_value {
     86        void ?{}( T&, T );
     87        T ?=?( T&, T );
     88        void ^?{}( T& );
     89};
     90
    7891// implicit increment, decrement if += defined, and implicit not if != defined
    7992
     
    8194// assignment, but is not an lvalue." Hence, return a value not a reference.
    8295static inline {
    83         forall( T | { T ?+=?( T &, one_t ); } )
     96        forall( T& | is_value(T) | { T ?+=?( T &, one_t ); } )
    8497        T ++?( T & x ) { return x += 1; }
    8598
    86         forall( T | { T ?+=?( T &, one_t ); } )
     99        forall( T& | is_value(T) | { T ?+=?( T &, one_t ); } )
    87100        T ?++( T & x ) { T tmp = x; x += 1; return tmp; }
    88101
    89         forall( T | { T ?-=?( T &, one_t ); } )
     102        forall( T& | is_value(T) | { T ?-=?( T &, one_t ); } )
    90103        T --?( T & x ) { return x -= 1; }
    91104
    92         forall( T | { T ?-=?( T &, one_t ); } )
     105        forall( T& | is_value(T) | { T ?-=?( T &, one_t ); } )
    93106        T ?--( T & x ) { T tmp = x; x -= 1; return tmp; }
    94107
    95         forall( T | { int ?!=?( T, zero_t ); } )
     108        forall( T& | is_value(T) | { int ?!=?( T, zero_t ); } )
    96109        int !?( T & x ) { return !( x != 0 ); }
    97110} // distribution
  • tests/zero_one.cfa

    rfd0a1799 r8e4f34e  
    3030S ?+=?( S & s, one_t ) { s.[i,j] += 1; return s; } // increment
    3131S ?-=?( S & s, one_t ) { s.[i,j] -= 1; return s; }
    32 S ++?( S & s ) { s += 1; return s; }
    3332int ?!=?( S s, zero_t ) { return s.i != 0 && s.j != 0; } // comparison
    3433void testInitAssignQueryIncrement() {
Note: See TracChangeset for help on using the changeset viewer.