Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symbol literals #23

Open
chronium opened this issue Nov 13, 2019 · 5 comments
Open

Symbol literals #23

chronium opened this issue Nov 13, 2019 · 5 comments

Comments

@chronium
Copy link
Owner

Add symbol literals for use with quoting and macro creation, dictionary keys and other constructs.
Symbols are primitive values, and can be composed.
They would also be useful for defining custom operators. For example, overloading the + operator could be the form of: fn :+ (self, other)

Symbol literals would have the form of: :[\S]+

Example

Given:

val sym = :test
val theMap = Map(:test => "Hello", :testcomp => "Hello World") // decide on key-value pair syntax

Extract the value:

// straight symbol argument
val hello = theMap :test
val hello2 = theMap(:test)

// compose symbol
val composed = sym :: :comp // creates symbol :testcomp

// symbol stored in variable
val helloWorld = theMap composed
val helloWorld2 = theMap(composed)
@SplittyDev
Copy link
Collaborator

SplittyDev commented Nov 13, 2019

Great idea, I loved this stuff in Ruby.

One thing that seems a bit weird to me is the operator overloads.
So you'd use :+ to overload the + operator, but use it as simply + later?

In that case I think simply fn + (self, other) would make more sense.

The parser can easily handle that special case after fn so you don't have to allow it for all identifiers at once.

@chronium
Copy link
Owner Author

Fair enough with operators being simple, instead of symbols. But maybe we could allow symbol function names for manual dynamic dispatch for example.

Another thing, what would we do for postfix operators?
With the current design ideas, prefix functions are any function that has a single parameter.
Infix functions are basically any function that takes two parameters, but what about postfix?
Maybe we could use symbol function names for postfix?

Example:

fn foo_self (self) // names can be symbols and literal symbols from now on as well

@prec(22) // specify precedence level
fn foo_inf (self, other)

fn :? (self) // defines postfix `?`

// usage
foo_self self_inst
self_inst.foo_self

self_inst foo_inf other_inst
self_inst.foo_inf other_inst
self_inst.foo_inf(other_inst)

self_inst? // calls postfix `?` on self

@SplittyDev
Copy link
Collaborator

If :? denotes postfix, how would you do prefix operators?
Would it be ?: then for ?self_inst?

@SplittyDev
Copy link
Collaborator

SplittyDev commented Nov 13, 2019

Maybe you could use the : symbol to differentiate operator placement.

fn ? (self, true, false): foo.? (a, b)
fn ?: self: ?foo
fn :? self: foo?

@chronium
Copy link
Owner Author

In the current design, prefix is any function that takes a single argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants