Re: Function Arguments with Multiple Types

2019-09-07 Thread Bert via Digitalmars-d-learn
On Friday, 6 September 2019 at 20:02:35 UTC, Bob4 wrote: Hi, I'm coming from a background in Python, without a lot of experience in statically typed languages, and I'm struggling to see how to make a certain function in D. This is what I have in Python: ``` from typing import Union Number

Re: Function Arguments with Multiple Types

2019-09-06 Thread Ali Çehreli via Digitalmars-d-learn
On 09/06/2019 01:46 PM, Bob4 wrote: > Thanks; this works, but I'm not sure why. Where does `T` come from? Well... I assumed this would make you research templates. ;) Here is one resource: http://ddili.org/ders/d.en/templates.html (T) means "this function is for any type; and I call that

Re: Function Arguments with Multiple Types

2019-09-06 Thread Bob4 via Digitalmars-d-learn
On Friday, 6 September 2019 at 20:16:58 UTC, Ali Çehreli wrote: On 09/06/2019 01:02 PM, Bob4 wrote: > I feel like it's wrong to rewrite identical functions over and over. Enter templates. :) auto clamp(T)(T value, T mini, T maxi) { if (value >= maxi) { return maxi; } if (value <=

Re: Function Arguments with Multiple Types

2019-09-06 Thread Ali Çehreli via Digitalmars-d-learn
On 09/06/2019 01:02 PM, Bob4 wrote: > I feel like it's wrong to rewrite identical functions over and over. Enter templates. :) auto clamp(T)(T value, T mini, T maxi) { if (value >= maxi) { return maxi; } if (value <= mini) { return mini; } return value; } unittest {

Function Arguments with Multiple Types

2019-09-06 Thread Bob4 via Digitalmars-d-learn
Hi, I'm coming from a background in Python, without a lot of experience in statically typed languages, and I'm struggling to see how to make a certain function in D. This is what I have in Python: ``` from typing import Union Number = Union[int, float] def clamp(value: Number, mini: