www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the difference between these template declaration forms?

reply cc <cc nevernet.com> writes:
Are these identical?  Or is there a different usage for the (T : 
something) form?
```d
	auto opCast(T)() if (is(T == bool)) {
		return _obj !is null;
	}
```
```d
	auto opCast(T : bool)() {
		return _obj !is null;
	}
```
May 15
parent evilrat <evilrat666 gmail.com> writes:
On Saturday, 15 May 2021 at 08:37:21 UTC, cc wrote:
 Are these identical?  Or is there a different usage for the (T 
 : something) form?
 ```d
 	auto opCast(T)() if (is(T == bool)) {
 		return _obj !is null;
 	}
 ```
They are the same in this case.
 ```d
 	auto opCast(T : bool)() {
 		return _obj !is null;
 	}
 ```
While the docs says it matches the most specialized type, which is bool in this case, IIRC it is actually takes anything convertible to bool despite what the docs says. You should double check it yourself. Anyway I would usually pick this form where possible as it yields cleaner error messages unlike arbitrary template constraint "stuff X doesnt takes Y, did you mean (basically X but not)" message that who knows what it wants you to do.
May 15