www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why some function are class-less?

reply Suliman <evermind live.ru> writes:
There is some functions that can be called without creation of 
class.
For example: http://vibed.org/api/vibe.core.log/

As I understand I can create write log in two way. First create 
instance of Logger, second simply call function.

Why it's done so? Just as shortcut? But is I create class it's 
look very poor http://vibed.org/api/vibe.core.log/FileLogger and 
I have only one needed method: ` put 	Writes part of a log line 
message. `
Nov 17 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/17/16 11:28 AM, Suliman wrote:
 There is some functions that can be called without creation of class.
 For example: http://vibed.org/api/vibe.core.log/

 As I understand I can create write log in two way. First create instance
 of Logger, second simply call function.

 Why it's done so? Just as shortcut? But is I create class it's look very
 poor http://vibed.org/api/vibe.core.log/FileLogger and I have only one
 needed method: ` put     Writes part of a log line message. `
D does not require classes to write functions. All a class member function is anyway is a function with an implied class instance parameter. -Steve
Nov 17 2016
parent reply Suliman <evermind live.ru> writes:
On Thursday, 17 November 2016 at 16:46:37 UTC, Steven 
Schveighoffer wrote:
 On 11/17/16 11:28 AM, Suliman wrote:
 There is some functions that can be called without creation of 
 class.
 For example: http://vibed.org/api/vibe.core.log/

 As I understand I can create write log in two way. First 
 create instance
 of Logger, second simply call function.

 Why it's done so? Just as shortcut? But is I create class it's 
 look very
 poor http://vibed.org/api/vibe.core.log/FileLogger and I have 
 only one
 needed method: ` put     Writes part of a log line message. `
D does not require classes to write functions. All a class member function is anyway is a function with an implied class instance parameter. -Steve
Ok, but what profit? Could you give me an example (for example with logger)?
Nov 17 2016
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Thursday, 17 November 2016 at 17:42:44 UTC, Suliman wrote:
 On Thursday, 17 November 2016 at 16:46:37 UTC, Steven 
 Schveighoffer wrote:
 On 11/17/16 11:28 AM, Suliman wrote:
 [...]
D does not require classes to write functions. All a class member function is anyway is a function with an implied class instance parameter. -Steve
Ok, but what profit? Could you give me an example (for example with logger)?
for example writeln. You would not want to create a stdio object just to call writeln.
Nov 17 2016
parent reply Suliman <evermind live.ru> writes:
On Thursday, 17 November 2016 at 17:45:31 UTC, Stefan Koch wrote:
 On Thursday, 17 November 2016 at 17:42:44 UTC, Suliman wrote:
 On Thursday, 17 November 2016 at 16:46:37 UTC, Steven 
 Schveighoffer wrote:
 On 11/17/16 11:28 AM, Suliman wrote:
 [...]
D does not require classes to write functions. All a class member function is anyway is a function with an implied class instance parameter. -Steve
Ok, but what profit? Could you give me an example (for example with logger)?
for example writeln. You would not want to create a stdio object just to call writeln.
Ok, but when the logger class may be more helpful that function usage?
Nov 17 2016
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Thursday, 17 November 2016 at 17:54:23 UTC, Suliman wrote:
 On Thursday, 17 November 2016 at 17:45:31 UTC, Stefan Koch 
 wrote:
 On Thursday, 17 November 2016 at 17:42:44 UTC, Suliman wrote:
 On Thursday, 17 November 2016 at 16:46:37 UTC, Steven 
 Schveighoffer wrote:
 On 11/17/16 11:28 AM, Suliman wrote:
 [...]
D does not require classes to write functions. All a class member function is anyway is a function with an implied class instance parameter. -Steve
Ok, but what profit? Could you give me an example (for example with logger)?
for example writeln. You would not want to create a stdio object just to call writeln.
Ok, but when the logger class may be more helpful that function usage?
If you log something you usually want to keep private state. Classes are useful for that.
Nov 17 2016
prev sibling parent reply Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
On Thursday, 17 November 2016 at 17:54:23 UTC, Suliman wrote:
 Ok, but when the logger class may be more helpful that function 
 usage?
You'd use the logger class when you need to make customizations or have multiple logging schemes. I'd expect Vibe.d allows you to override the global logging object. You could then inherit from the Logger class, customize the behavior and assign it to the global logger which is used by all the libraries which just call the log() function.
Nov 17 2016
parent reply Suliman <evermind live.ru> writes:
On Thursday, 17 November 2016 at 18:02:02 UTC, Jesse Phillips 
wrote:
 On Thursday, 17 November 2016 at 17:54:23 UTC, Suliman wrote:
 Ok, but when the logger class may be more helpful that 
 function usage?
You'd use the logger class when you need to make customizations or have multiple logging schemes. I'd expect Vibe.d allows you to override the global logging object. You could then inherit from the Logger class, customize the behavior and assign it to the global logger which is used by all the libraries which just call the log() function.
For example I need to write all logs to file. I see this function, that seem set some global file name. "setLogFile Sets a log file for disk file logging." But would it name accessible from anywhere or it would be visible inly from this scope? If yes is there any way to make it's global?
Nov 17 2016
parent Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
On Thursday, 17 November 2016 at 18:40:12 UTC, Suliman wrote:
 For example I need to write all logs to file. I see this 
 function, that seem set some global file name.
 "setLogFile 	Sets a log file for disk file logging."

 But would it name accessible from anywhere or it would be 
 visible inly from this scope? If yes is there any way to make 
 it's global?
It is going to be global if it is a free function and doesn't take/modify a logging object.
Nov 17 2016