You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i like the idea of Castable. but i don't like to have too much public functions without an "Namespace" aka Class flying around like cast, castable, match.
i prefer it more encapsulated in one class and also to prefix the first closure also with match to get it uniform with the other matches
i rewrite the class like this:
/** Chainable type representing the value matching different types */publicenumCastable<Wrapped>{case value(Wrapped)/** Wraps a value in Castable. - Parameter x: Value to wrap. - Returns: Value wrapped by Castable */init(_ data:Wrapped){self=.value(data)}/** Casts a value from Type to Result? - Parameter value: Value to be casted - Returns: *.some(value: Result)* if the value could be casted, *.none* otherwise */privatefunc cast<Type,Result>(_ value:Type)->Result?{return value as?Result}/** Casts a value to function argument type and calls a function, if the cast was successfull. - Parameter f: Function to call, if the *.value* in can be be casted to function argument - Returns: Castable wrapping the same value */@discardableResultpublicfunc match<Subject>(f:(Subject)->())->Castable<Wrapped>{
switch self{caselet.value(x):cast(x).map{f($0)}return.value(x)}}/** Extracts a value from Castable - Returns: Value wrapped by Castable */publicfunc extract()->Wrapped{
switch self{caselet.value(x):return x
}}}
Well, the thing is, that I prefer using public short functions.
Secondly, cast specifically is used for type inference, I use to avoid as? operators with types, inferring types instead.. See my articles for more details: Type Inference, Chainable Casting
And the last, but not the least, you see, the thing is, that cutting down the functionality is overall worse, then expanding. That's breaking changes inside my own projects and projects other people rely on.
i like the idea of Castable. but i don't like to have too much public functions without an "Namespace" aka Class flying around like
cast
,castable
,match
.i prefer it more encapsulated in one class and also to prefix the first closure also with
match
to get it uniform with the othermatch
esi rewrite the class like this:
to test it in Playground
Output:
The text was updated successfully, but these errors were encountered: