>his """language""" doesn't support generic returns
His """language""" doesn't support generic returns
method overloading is much cleaner, and in cases like this one there's always metaprogramming
Nim looks pretty nice. I'm learning it.
>identity function that only works on strings and integers
>identity function with a type that isn't T -> T
Here you have a better example of what I was getting at
This isn't a better example because overloading is a much better fit.
But it does
myFun :: Either Int String -> Either Int String
myFun = id
Also, I don't see how the word "generic" applies to it. It's just a sum of types.
>overloading
you have to go back
>not using the best tool for the job
never reply to me again. you disgust me
Make me.
(fun x -> x)
generic
type T_ is private;
function myFunc(X : in T_) return T_;
Is int or string an anonymous sum type?
You're the disgusting one.
Please give me a use-case.
just use dynamic typing :)
fun = lambda x: x
assert fun(4) == 4
assert fun(':)') == ':)'
generalized sorting function, generalized heap or generalized stack objects.
just use void pointers. So much cleaner and more elegant.
>confusing tagged unions for open polymorphism
Begone cnile.
>void pointers
>clean and elegant
Pick one
What's wrong with templates?
>his """language""" doesn't support multiple return values
def piv l
return l[0], l.select{|i| il[0]}
end
pivot, smaller, bigger = piv [6,3,9,7,2,1,8,4,5]
So what, you gotta put "int or string" everywhere? What if you want to declare a variable of the same type in the body of the function?
C++ template syntax seem cleaner.
>having types but Object and Function
>having types besides function
You can define a type alias.
>C++ template syntax seem cleaner.
You can't do that in C++ without using a mess of template hacks.
Templates are something different. But you can achieve what OP does with std::variant.
mah nigga, learnin that shit once these goddamn finals are over
>You can't do that in C++ without using a mess of template hacks.
Wouldn't a template + static asserts that T is string or int work?
>his
Hers.
Thank you for your understanding btw.
>+ static asserts that T is string or int work
Try actually writing that code. is a much more reasonable solution but it's still a horrible template hack built into the standard library.
Ah yeah I guess the "or" part is where the difficulty lies. Does std::variant have any runtime overhead?
>it's still a horrible template hack
You could do it with nothing more than an enum and a union senpai.
Nope. It doesn't do any dynamic allocation by itself. The internal representation is very simple.
>The internal representation is very simple
It's a tagged union so there is "some" overhead.
>You could do it with nothing more than an enum and a union senpai.
The horrible template hack hides the complexity from the user of the function, enum + union does not.
>You can't do that in C++ without using a mess of template hacks.
Hmm?
template
T myProc(T x) {
return x;
}
assert(myProc(4) == 4);
assert(myProc(":)") == ":)");
Last one should work if the compiler is handling static strings properly and you don't mind a little UB. Or just use strcmp.
No more overhead than if you were doing it yourself. I can't think of a simpler way of representing a variant type than enum + union, barring "intrusive" representations like null pointers.
You clearly don't understand what OP is demonstrating. He did a terrible job of it, any way.
The point is that a variable can be declared to be a member of a finite set of types. It's a useful concept, which is why C++ has std::variant.
>The point is that a variable can be declared to be a member of a finite set of types.
std::enable_if exists. But yes, I see your point.
That's different. You can't have a struct member whose type is determined at runtime to be string or int by using std::enable_if_t.